# Grading with Maven

### Overview

This guide shows how to set up a Maven-based Java programming assignment in CodeGrade, using instructor-written JUnit tests and AutoTest for automated grading.&#x20;

***

### Configuring AutoTest Setup

#### Install Java and Maven

In **AutoTest > Setup**:

* Add the **Install Java** block (or use the script below).
* Add a **Script** block to install Java 17, Maven 3.8.4, and `junitparser`:

{% code lineNumbers="true" %}

```bash
sudo apt update
sudo apt install openjdk-17-jre -y

wget https://archive.apache.org/dist/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz -P /tmp/
sudo tar xf /tmp/apache-maven-3.8.4-bin.tar.gz -C /opt
sudo ln -s /opt/apache-maven-3.8.4/ /opt/maven
sudo rm /usr/bin/mvn
sudo ln -s /opt/maven/bin/mvn /usr/bin/mvn

pip3 install junitparser
```

{% endcode %}

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2FsO3IvGik4BtgsF6XJh1e%2FScreenshot%202026-01-20%20at%2014.24.20.png?alt=media&#x26;token=b674c851-aac1-4feb-977c-f95b9a60862d" alt=""><figcaption><p>AutoTest Setup for Maven assignments</p></figcaption></figure>

***

### Configuring AutoTest Tests

#### 1. Upload and prepare instructor tests

* Upload your test files using an **Upload Files** block (e.g., `CircleTest.java`).
* Use a **Script** block to move them:

```bash
mkdir -p src/test/java/com/example/shapes
cp $UPLOADED_FILES/*.java src/test/java/com/example/shapes
```

{% hint style="warning" %}
Block student-submitted test files in **General Settings > Hand-in Requirements** (e.g., deny `*Test.java`).
{% endhint %}

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2F5j1R89kOQi4VXFJeXspu%2FScreenshot%202026-01-20%20at%2014.26.04.png?alt=media&#x26;token=34a81dfe-4ee1-4f2a-8fab-a0f5f81e1cc3" alt=""><figcaption><p>Upload and prepare tests</p></figcaption></figure>

#### 2. Prepare build and classpath

Add a **Script** block to compile and collect dependencies:

```bash
#!/usr/bin/env bash
set -euo pipefail

mvn -q -DskipTests=true clean package
mvn -q dependency:copy-dependencies -DoutputDirectory=target/dependency -DincludeScope=test

DEP_JARS="$(ls target/dependency/*.jar 2>/dev/null | tr '\n' ':' || true)"
SELF_JAR="$(ls target/*.jar 2>/dev/null | tr '\n' ':' || true)"
JAR_PATHS="${DEP_JARS}${SELF_JAR}target/classes:target/test-classes"
CG_CP="$(cg junit5 get-classpath || true)"

echo "export CLASSPATH=\"${JAR_PATHS}${CG_CP:+:${CG_CP}}:.\"" >> ~/.cg_bash_env
echo 'export MAIN_CLASS="com.example.Main"' >> ~/.cg_bash_env
```

{% hint style="info" %}
This ensures all compiled classes and dependencies are available to the test phase.
{% endhint %}

#### 3. Running Maven tests

Use **Connect Rubric** + **Custom Test** blocks for each test class. Each test block can link to a separate rubric category for grading.

```bash
mvn -q -Dtest=CircleTest test || true
cg junitxml target/surefire-reports/TEST-CircleTest.xml
```

{% hint style="info" %}
The project is compiled during the setup phase. Maven automatically skips recompilation when there are no changes.
{% endhint %}

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2FS2oSgKK1DYuikSD9HzBp%2FScreenshot%202026-01-20%20at%2014.22.20.png?alt=media&#x26;token=54afae21-49fd-4293-b62c-275826b08dfd" alt=""><figcaption><p>Running Maven tests<br></p></figcaption></figure>

***

### Troubleshooting Maven Autograding in CodeGrade

This section lists common issues you may encounter when setting up Maven-based Java assignments in CodeGrade, along with quick solutions.

<details>

<summary><strong>Maven or Java not found</strong></summary>

**Symptom:** AutoTest logs show `mvn: command not found` or `java: command not found`

**Fix:**

* Ensure Java and Maven are installed in the Setup phase.
* Double-check the setup script includes the correct symbolic links to Maven:

```bash
sudo ln -s /opt/maven/bin/mvn /usr/bin/mvn
```

</details>

<details>

<summary><strong>Tests not detected</strong></summary>

**Symptom:** Maven runs without executing any tests.

**Fixes:**

* Ensure test class names match `*Test.java` or `*Tests.java`.
* Confirm test files are copied into `src/test/java/...`
* If tests use packages, ensure folders match package structure.
* Use the `-Dtest=ClassName` flag to target specific test classes in test blocks.

</details>

<details>

<summary><strong>Test failures without output</strong></summary>

**Symptom:** AutoTest runs but doesn’t show detailed feedback.

**Fix:**

* Verify `cg junitxml target/surefire-reports/*.xml` is included after `mvn test`.
* Ensure the Surefire plugin outputs XML (default behavior).
* Avoid cleaning (`mvn clean`) between test blocks unless needed.

</details>

<details>

<summary><strong>Classpath errors or missing dependencies</strong></summary>

**Symptom:** `ClassNotFoundException` or build errors for imported classes.

**Fixes:**

* Run `mvn dependency:copy-dependencies -DincludeScope=test` in Setup.
* Confirm your `pom.xml` includes required dependencies.
* Use the environment setup script to populate `CLASSPATH`.

</details>

<details>

<summary><strong>Timeouts or hanging tests</strong></summary>

**Symptom:** Tests hang or AutoTest fails due to long execution.

**Fixes:**

* Use JUnit timeouts (e.g., `@Test(timeout = 2000)` or `assertTimeout()` for JUnit 5).
* Review student code for infinite loops or blocking I/O.
* Set step-level timeouts in AutoTest test block settings.

</details>

#### Still Need Help?

For assistance with custom cases, reach out to <support@codegrade.com> and share a relevant snapshot or example submission.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.codegrade.com/automatic-grading-guides/java/grading-with-maven.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
