Advanced Java autograding
Discover the advanced autograding options available for Java assignments
Last updated
Discover the advanced autograding options available for Java assignments
Last updated
In this guide, we explore the advanced grading options available for Java assignments. For more information about setting up a Java assignment from scratch, see
Junit5 is an industry-standard unit testing framework for Java. It is particularly useful for grading assignments that require students to write functions and classes. Using Junit5 unit tests offers several advantages over conventional IO tests including the ability to use assertions, parametrize test cases, and provide better feedback for students. Consider the example submission below:
We can create robust test cases to check this submission using Junit5 assertions. Additionally, we can use decorators such as @displayName
to make our test cases more readable and to increase the weight of individual test cases.
To make running JUnit5 tests in CodeGrade simpler, we created the cg junit5
command. This can be used to install JUnit5, automatically compile and run your JUnit5 test cases, or fetch the JUnit5 Jar files to run JUnit5 with your own custom specifications.
Running the command cg junit5 --help
shows the following information:
The cg junit5
command works in combination with the Custom Test block by parsing the output of the JUnit5 test. The Custom Test block then renders the test results beautifully and makes it easy to read and interpret, as shown in the image below.
In the AutoTest settings, navigate to the Tests tab.
Add an Upload Files block to your AutoTest configuration and upload your JUnit5 test files (eg. TestCalculator.java).
Add an Allow Internet block and a Script block to your AutoTest configuration. Nest the Script block within the Allow Internet block. Download and install JUnit5 using the following command.
Add a Connect Rubric Block and a Custom Test block to your AutoTest configuration. Nest the Custom Test block within the Connect rubric block. Run the JUnit5 tests using the following command, using your Junit5 test file (eg. TestCalculator.java).
Build and publish your snapshot
JUnit5 has the known issue that it isn't possible to specify the order in which test cases are run. As a result, the results for each test case may displayed in a seemingly random order.
Checkstyle is an industry-standard linter for Java that allows you to enforce rules from various style guides, including the Sun and Google style guides. It is a useful tool for enforcing code styling best practices for beginner programmers.
The Checkstyle block is an off-the-shelf testing tool that doesn't require any additional setup or configuration other than providing the name of the file to be tested. The Checkstyle block generates comments directly on students' code. Each target line is highlighted according to the severity of the comment and the comment can be read by hovering over the line number with the mouse cursor. The Checkstyle block also works in the built-in editor making it a powerful combination for beginners.
Although no setup is required, you can select from three readily available style guides: Checkstyle's default style guide, Sun style guide, and Google style guide. In addition, you can also update the configuration for each of these options to ignore or include any of the rules. Aside from style guides, you can also adjust the percentage of points deducted for each comment placed on a submission depending on its severity.
These advanced Java testing options open the door to more rigorous testing methods and better student feedback. However, these are just the most commonly used methods. There are many more testing tools and packages available that you can use with some simple setup. For more information, contact us at support@codegrade.com.