Skip to main content

Creating a JUnit Autograder

Learn how to use the JUnit framework with your custom coding exercises

Written by Sara Jenis
Updated over a week ago
Banner: This is a free feature

πŸ’‘ Check out this page for more detailed tutorials, tips, and code samples for creating JUnit Autograders.

Overview

This is a guide to creating a JUnit autograder on CodeHS, detailing how to program JUnit-based tests and which assertion methods are available. This guide assumes prior knowledge of basic autograder functionality and focuses specifically on writing and structuring JUnit tests.

JUnit autograders rely on testing methods, constructors, and object behavior from the class files students create. JUnit autograders will validate correctness by executing unit tests.

For more information on creating/editing autograders, feel free to check out this article: Adding Custom Autograders and Preset Configs πŸ“–

Building a JUnit Autograder

Adding a JUnit Autograder File

Java autograders are created by default when clicking the add button in a custom Java assignment's autograder editor. To add a JUnit autograder:

  1. Click the dropdown arrow next to the '+Add' icon in the autograder editor.

  2. Choose 'Add a JUnit Autograder'.

image displaying how to add a junit autograder file

Autograder Starter Code

When you add a JUnit Autograder to an assignment, it comes preloaded with starter code containing example test cases in JUnitSampleTest.java. You should only need to edit JUnitExampleTest.java. The other files should remain in the autograder to ensure the tests are run correctly.

image displaying the junit boilerplate starter code sample tests

Each test includes:

  • @Test β†’ Marks the method as a test

  • @DisplayName β†’ Student-facing label

  • A void method with no parameters

  • At least one assertion

JUnit Assertion Methods

JUnit tests rely on assertion methods from the JUnit framework.

assertEquals

Compares two values.

assertEquals(expected, actual);

Example:

@Test
@DisplayName("doubleNum returns double the input")
void testDoubleNum() {
assertEquals(6, StudentClassName.doubleNum(3));
}

Adding Custom Messages

assertEquals(6, StudentClassName.doubleNum(3),
"doubleNum(3) should return 6");

Floating-Point Comparisons

When testing double or float, include a delta (tolerance):

assertEquals(expected, actual, delta);

Example:

assertEquals(25.0, book.percentComplete(), 0.01);

Without a delta, floating-point comparisons will not compile.

assertTrue and assertFalse

Used to test boolean expressions.

assertTrue(StudentClassName.isEven(4)); assertFalse(StudentClassName.isEven(5));

assertNotNull and assertNull

Used to verify object references.

Book book = new Book("Java 101", 200);
assertNotNull(book);

Book emptyBook = null;
assertNull(emptyBook);

assertSame and assertNotSame

Used to verify object identity (reference equality).

Book book1 = new Book("Java 101", 200);
Book book2 = book1;
assertSame(book1, book2);

Book book3 = new Book("Java 101", 200);
assertNotSame(book1, book3);

Testing Student Methods

Testing a Static Method

If students write:

public static int doubleNum(int x) {
return x * 2;
}

You can test it directly:

@Test
@DisplayName("Check doubleNum method")
void testDoubleNum() {
assertEquals(4, StudentClassName.doubleNum(2));
}

Testing Instance Methods

If students create a class:

Book book = new Book("Java 101", 200);

You can test its methods:

@Test
@DisplayName("Test Book behavior")
void testBook() {
Book book = new Book("Java 101", 200);

assertEquals("Java 101", book.getTitle());
assertEquals(200, book.getPages());
assertEquals(0.0, book.percentComplete(), 0.01);

book.readPages(50);
assertEquals(25.0, book.percentComplete(), 0.01);
}

Running Multiple Assertions in One Test

You may include multiple assertions in a single test:

@Test
@DisplayName("Multiple doubleNum cases")
void testMultipleCases() {
assertEquals(4, StudentClassName.doubleNum(2));
assertEquals(10, StudentClassName.doubleNum(5));
assertEquals(0, StudentClassName.doubleNum(0));
}

⚠️ If one assertion fails, the test will stop and the remaining assertions won’t run.

Getting Student Output and Reading Student Code

Assignments that require validating console output, reading student source code, or checking for keywords/variable names should use the Java Autograder instead.

Creating a JUnit Autograder Demo


Still have questions? Contact our team at support@codehs.com to learn more!

Did this answer your question?