Java: JUnit

Running JUnit scripts in the codePost Autograder

Vinay avatar
Written by Vinay
Updated over a week ago

Setup:

Environment: 

  • Language: Java

  • Build Type: Custom - Ubuntu

  • Packages: apt-get install -y junit4; apt-get install -y default-jdk

  • Run Script: None

  • Helper Files: Upload your JUnit Test script as a helper file

Tests:

  1. Open file mode, and click "Add File". Create a new Test File

  2. In your Test File, add the following two commands:

# Compile all java files in the directory, with junit in the classpath
javac -cp /usr/share/java/junit4-4.12.jar *.java

# Run the Junit test with the runner
java  -cp /usr/share/java/junit4-4.12.jar:. org.junit.runner.JUnitCore <TestName>

Settings

  • Turn on "dump test outputs to  _tests.txt. This stores the JUnit outputs into a file in each student's submissions

  • If you want students to see the output of all tests when they submit, you'll also want to turn on "Expose outputs to students on submit".

Result

File Mode view of  running Two JUnit test scripts

The test results from the code console

Advanced

Packages

If you have nested directories, or student files are part of a package, you'll have to edit these commands. For example, if your students are submitting all files with the package a02 , your commands would look like this:

# Create a temporary directory
mkdir a02

# Compile the classes to the a02 directory
classpathjavac -cp /usr/share/java/junit4-4.12.jar *.java -d /a02

# Add a02 to the classpath, and run the test
java  -cp /usr/share/java/junit4-4.12.jar:.:a02 org.junit.runner.JUnitCore a02.<TestName>

JUnit5

If you require using junit5 with the codePost autograder, you may follow these instructions to install it on your instance:

  1. Set up an Ubuntu container with default-jdk (same as in the junit4 instructions)

  2. Upload it as a helper file. It might take a bit of time, as it's a big file.

  3. Replace the file mode test file commands with the following two lines:

javac -d . -cp /junit-platform-console-standalone-1.6.0.jar <filename>.java
java -jar /junit-platform-console-standalone-1.6.0.jar --class-path . --scan-class-path

These instructions compile the java file to the root directory with the junit5 jar and then run it. This method is using the command line to run junit5 files, which you can learn more about here.

Did this answer your question?