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:

cg junit5 install
circle-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.

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:

#!/usr/bin/env bash
cg java compile $STUDENT/myapp/*.java
mv $UPLOADED_FILES/*.java $STUDENT/myapp
An Upload Files block with the teacher’s test file, followed by a Compilation script.
circle-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.

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:

A Connect Rubric containing a Custom Test block that compiles and runs the teacher’s tests.
circle-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.

circle-exclamation

Conclusion

You have set up an automatically graded assignment that handles package-structured student code. Explore more details on JUnit5 in CodeGradearrow-up-right or on running Checkstylearrow-up-right alongside your tests. For any questions about custom setups, reach out to our support team at [email protected].

Last updated