# Grading Java package projects

More advanced Java assignments often ask students to organise their code into packages. When a student's code lives under a package such as `myapp`, any instructor tests you want to run against it must sit inside the same package folder so that `javac` and JUnit can resolve the imports correctly.

This guide walks through the pattern: upload the instructor's JUnit 5 test file, compile the student's code, move the test into the student's package folder, and run it.

### Step 1: Install JUnit 5

Before setting up your tests, you need to install JUnit5 in the AutoTest environment.

1. Navigate to the **Setup** tab under the AutoTest settings.
2. Add a **Script** block and paste the following:

```bash
cg junit5 install
```

{% hint style="info" %}
`cg junit5 install` downloads the JUnit 5 platform into the AutoTest environment and makes its classpath available via `cg junit5 get-classpath`. This only needs to be run once in Setup — all subsequent test blocks in the same AutoTest configuration can reference it.
{% endhint %}

### Step 2: Compile the student code and place the instructor tests

1. Navigate to the **Tests** tab under the AutoTest settings.
2. Add an **Upload Files** block and attach your instructor test file (e.g. `CalculatorTest.java`).
3. Add a **Script** block immediately after and paste the following:

```bash
#!/usr/bin/env bash
cg java compile $STUDENT/myapp/*.java
mv $UPLOADED_FILES/*.java $STUDENT/myapp
```

<div align="center"><figure><img src="/files/scGXHrGZqbHibEhBKTV3" alt=""><figcaption><p>An Upload Files block with the teacher’s test file, followed by a Compilation script.</p></figcaption></figure></div>

{% hint style="info" %}
`cg java compile` compiles the student's Java sources and attaches any compiler errors as inline comments on their submission. Moving the uploaded test into `$STUDENT/myapp` places it alongside the student's classes inside the same package.
{% endhint %}

### Step 3: Run the JUnit 5 tests

1. Add a **Connect Rubric** block and select the appropriate rubric category from the drop-down menu.
2. Add a **Custom Test** block and nest it within the **Connect Rubric** block.
3. Paste the following into the editor:

```bash
# Compile the test file
javac -cp "/home/codegrade/student:/home/codegrade/student/myapp:`cg junit5 get-classpath`" \
  myapp/CalculatorTest.java

# Run the JUnit tests and write the report
java -cp "/home/codegrade/student:/home/codegrade/student/myapp:`cg junit5 get-classpath`" \
  org.junit.platform.console.ConsoleLauncher \
  --select-class myapp.CalculatorTest \
  --fail-if-no-tests \
  --reports-dir '/tmp/reports'

# Gather the results and score
cg junitxml /tmp/reports/*.xml
```

<figure><img src="/files/DhhMdFpP4oFahQF3jDRa" alt=""><figcaption><p>A Connect Rubric containing a Custom Test block that compiles and runs the teacher’s tests.</p></figcaption></figure>

{% hint style="info" %}
`cg junit5 get-classpath` returns the classpath for the JUnit 5 platform installed in Step 1. `cg junitxml` parses the generated JUnit XML report and awards rubric points based on the pass/fail ratio.
{% endhint %}

{% hint style="warning" %}
If your students organise their work across multiple packages, adjust the classpath entries and the `--select-class` argument accordingly — for example, `myapp.utils.StringUtilsTest`.
{% endhint %}

### Conclusion

You have set up an automatically graded assignment that handles package-structured student code. Explore more details on [JUnit5 in CodeGrade](https://help.codegrade.com/automatic-grading-guides/java/advanced-java-autograding#junit5) or on running [Checkstyle](https://help.codegrade.com/automatic-grading-guides/java/advanced-java-autograding#checkstyle) alongside your tests. For any questions about custom setups, reach out to our support team at <support@codegrade.com>.


---

# 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-java-package-projects.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.
