CodeGrade Help
Go to websiteAPI docsContact us
  • 🏠CodeGrade Help Center
  • ❓FAQ
    • Using CodeGrade in Safari
    • Using sections
    • Configuring roles of members
    • Archiving your course
    • Adding new members
    • Releasing grades and feedback
    • Exporting Submissions
    • Choosing a grading scale
    • Creating course-wide snippets
    • Course Gradebook
    • Allowing students to hand in after the deadline
  • Use built-in content
    • 👥Community Library
    • 🐍Introduction to Python Curriculum
  • Create your own assignments
    • 1️⃣Build your assignment
      • ➕Create assignment
        • ➕Standalone
        • ➕In Blackboard
        • ➕In Brightspace
        • ➕In Canvas
        • ➕In Moodle
        • ➕In Sakai
        • ➕In Open edX
        • 💾Importing a previous assignment
      • ⚙️General settings
      • 📋Add Rubric
      • 🤖Add Automatic Grading
      • 🧑‍🎓Student View
    • 2️⃣Grade your assignment
      • ✏️Give Feedback
      • 💯Give a Grade
    • 3️⃣Analyze your assignment
      • 🕵️‍♂️Detect Plagiarism
      • 📊Analytics
      • 🎓View and export grades
    • *️⃣Other features
      • AI Assistant
      • 👥Peer Feedback
      • 🫂Group Assignments
      • 🙋Collaborative Grading
      • 🕶️Anonymous Grading
      • 🛡️Manage Course Permissions
      • 📬Hand In Requirements
  • Automatic Grading Guides
    • 🏗️AutoTest V2 Blocks
    • ✏️Quizzes
      • ❓Multiple Choice Question
      • ❓Select All Question
      • 💻Coding Question
    • 🐍Python
      • 🐍Create your first Python assignment
      • 🐍Advanced Python autograding
    • ☕Java
      • ☕Create your first Java assignment
      • ☕Advanced Java autograding
    • 📘Jupyter Notebook
    • 🐬MySQL
    • 🌐Web Development
      • 🌐Web Development with Selenium
      • 🎨Automatically grading CSS
    • 🟨JavaScript
      • 🟨Create your first JavaScript assignment
      • 🟨Advanced JavaScript autograding
    • 📊R
    • ©️C
      • ©️Create your first C assignment
      • ©️Advanced C Autograding
    • 🖥️C#
      • 🖥️Create your first C# assignment
      • 🖥️Advanced C# autograding
    • ➕C++
      • ➕Create your first C++ assignment
      • ➕Advanced C++ autograding
    • 🐘PHP
      • 🐘Create your first PHP assignment
      • 🐘Advanced PHP autograding
    • 🏗️Code Structure Tests with Semgrep
  • For students
    • 🚀Getting started
      • 🚀Getting started in CodeGrade
      • 🚀Getting started in Blackboard
      • 🚀Getting started in Brightspace
      • 🚀Getting started in Canvas
      • 🚀Getting started in Moodle
      • 🤷‍♂️I forgot my CodeGrade username / password
    • 🧬Advanced Features
      • 👥Handing in with a group
      • 📥Handing in using Git
      • 📝Giving Peer Feedback
      • 🏆Doing a Final Exam
      • ❓Asking Questions
      • 💳Enrolling in a paid course
      • 🎟️Using a coupon to enroll in a course
      • ⏪Refunding a paid course
  • 📘APIv2: Typescript
  • 🐍APIv1: Python
  • 🤖APIv1: Docs
  • 🌐Our Website
  • ✉️Contact us
Powered by GitBook
On this page
  • JUnit5
  • Junit5 Block
  • Instructions
  • Checkstyle
  • Conclusion
  1. Automatic Grading Guides
  2. Java

Advanced Java autograding

Discover the advanced autograding options available for Java assignments

PreviousCreate your first Java assignmentNextJupyter Notebook

Last updated 3 months ago

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

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:

Calculator.java
import java.util.Scanner;
public class Calculator {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("Select operator: ");
    String operator = sc.nextLine();
    System.out.print("Enter two numbers: ");
    int num1 = sc.nextInt();
    int num2 = sc.nextInt();

    if (operator.equals("add")) {
        add(num1, num2);
    } else if (operator.equals("subtract")) {
        subtract(num1, num2);
    } else if (operator.equals("multiply")) {
        multiply(num1, num2);
    } else if(operator.equals("divide")) {
        divide(num1, num2);
    } else {
      System.out.println("The method you entered doesn't exist");
    }
  }

  public static float add(float numberOne, float numberTwo) {
    System.out.println(numberOne + numberTwo);
    return numberOne + numberTwo;
  }

  public static float subtract(float numberOne, float numberTwo) {
    System.out.println(numberOne - numberTwo);
    return numberOne - numberTwo;
  }

  public static float multiply(float numberOne, float numberTwo) {
    System.out.println(numberOne * numberTwo);
    return numberOne * numberTwo;
  }

  public static float divide(int numberOne, int numberTwo) {
    System.out.println(numberOne / numberTwo);
    return numberOne / numberTwo; // Should return ArithmeticException
  }
}

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.

TestCalculator.java
import java.util.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
public class TestCalculator {
  @Test
  @DisplayName("Check whether the addition function returns the expected result.")
  public void test_add() {
    Assertions.assertEquals( 5, Calculator.add(2, 3), 0.2);
    Assertions.assertEquals( 0, Calculator.add(1, -1), 0.2);
    Assertions.assertEquals(-2, Calculator.add(-1, -1), 0.2);
  }
  @Test
  @DisplayName("Check whether the subtraction function returns the expected result")
  public void test_subtract() {
    Assertions.assertEquals( 3, Calculator.subtract(5, 2), 0.2);
    Assertions.assertEquals( 2, Calculator.subtract(1, -1), 0.2);
    Assertions.assertEquals( 0, Calculator.subtract(-1, -1), 0.2);
  }
  @Test
  @DisplayName("Check whether the multiply function returns the expected result")
  public void test_multiply() {
    Assertions.assertEquals( 6, Calculator.multiply(2, 3), 0.2);
    Assertions.assertEquals(-1, Calculator.multiply(1, -1), 0.2);
    Assertions.assertEquals( 1, Calculator.multiply(-1, -1), 0.2);
  }
  @Test
  @DisplayName("[2] Check whether the divide function returns the expected result and throws an ArithmeticException when dividing by 0 (weight 2)")
  public void test_divide() {
    Assertions.assertEquals( 5, Calculator.divide(10, 2), 0.2);
    Assertions.assertEquals(-1, Calculator.divide(1, -1), 0.2);
    Assertions.assertEquals( 1, Calculator.divide(-1, -1), 0.2);
    Assertions.assertThrows(ArithmeticException.class, () -> {
      Calculator.divide(10, 0);
    });
  }
}

Junit5 Block

To make running JUnit5 tests in CodeGrade simpler, we created the Junit5 block. 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.

The block then renders the test results beautifully and makes it easy to read and interpret, as shown in the image below.

Instructions

  1. In the AutoTest settings, navigate to the Tests tab.

  2. Add a Connect Rubric Block and a Junit5 block to your AutoTest configuration. Nest the Junit5 block within the Connect rubric block.

  3. Copy the contents of your JUnit 5 test file (e.g., TestCalculator.java) and paste them into the Junit5 block's editor.

  4. Ensure that the Test file name matches the class name in the editor.

  5. 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

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.

Conclusion

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.

☕
☕
☕Create your first Java assignment