Skip to content

JUnit5/Maven Tests

The JUnit Maven tester runs JUnit 5 tests via the mvn command line tool. JUnit Jupiter assertions, Hamcrest matchers and assertions, and Mockito mocks are available.

Test Structure


Folder Structure

JUnit5 tests will be added to src/test/java/com/codecademy/ccapplication/ when run. This file is overwritten with the checkpoint test every time checkpoints are run. The test directory does not need to be added to the default workspace, the LE will create this directory if it does not exist when tests are run.

If learners are to run commands from the terminal and you do not want them to trigger tests, make sure to set the "Prevent Checkpoint Run" toggle on the terminal component in Author.

Test File Structure

All tests should be correctly scoped to the com.codecademy.ccapplication package and have a single CurriculumTest class.

If there is no CurriculumTest class, your test will not run as expected!

package com.codecademy.ccapplication;

class CurriculumTest {

Unintentional Errors in Tests and Defensive Testing

If there are compile errors in tests, the first failure should be revealed in the normal checkpoint error UI.

This can occur commonly by testing learner-defined methods that may not yet exist on the class. To avoid this issue, structure your test defensively:

  1. Don't directly call that method but rather use getter methods, e.g. getDeclaredMethod, to fetch it.
  2. Wrap your code in try / catch blocks.
class CurriculumTest {
  void example_verify_book_method() throws NoSuchMethodException {
    // We expect the learner to add a class method with the following signature:
    //        public Book getBook(@PathVariable Long id)

    try {
      // Use a getter to ensure that there is no compile error from BookController.getBook()
      Method getBookMethod = BookController.class.getDeclaredMethod("getBook", Long.class);
      // verify there exists a method argument with the expected type...
      Parameter idParameter =
        // If the method does not yet exist during the test runtime, this will throw a NoSuchMethodException to be caught below
                .filter(p -> p.getType().equals(Long.class))

    } catch (NoSuchMethodException e) {
      // Manually create an assertion error with friendly message
      Assertions.assertTrue(false, "Define the `getBook()` method.");

Test Troubleshooting

If you are not seeing the results you'd expect from running checkpoints normally via running code, you can manually run a test command from a terminal component. Save/run code to load the proper checkpoint file into the workspace (or manually edit src/test/java/com/codecademy/ccapplication/ and run the test in the terminal with.

mvn -o -Dtest=CurriculumTest clean test

If you see failing tests in the raw test output but the LE is handling it differently, please document and file a report with engineering. JUnit/Maven test reporting can have subtle output formatting differences, and test parsers may need to be updated.

Example Tests

Coming Soon

Other Guidelines

  • Actual code (real variable names, language syntax, etc. ) in feedback messages should be code-ticked (`)
  • JUnit Maven tests should follow Test standards.