7  6L. Writing JUnit Tests - 1

7.1 Pre-requisites

Please go through the writing JUnit tests chapter, mutation testing chapter, the List of Mutators chapter, and the slides on Mutation Testing from the lecture.

7.2 Project Overview

Here is the list of files in the Calculator Package:

.
├── src 
│   ├── Calculator
│   │   └── Calculator.java
└── test
    └── Calculator
        └── CalculatorTest.java

7.3 Detailed Instructions

  1. The Calculator.Calculator class is fully implemented and contains a list of methods for each basic arithmetic operation. The only method that is different from what you’d expect is divide(), which throws an exception if someone tries to divide by zero. This is interesting because when you write tests, you will have to make sure your test tests the fact that exceptions are being thrown when expected and not being thrown when not expected.

  2. The Calculator.CalculatorTest class inside test/Calculator/CalculatorTest.java is empty. Let’s talk about how to start writing this class.

  3. In order to be able to write tests, you need to import a bunch of methods called assertions from a library called JUnit. Here are some example assert methods:

    • assertEquals: takes two arguments, and checks if they are equal. If unequal, the test fails.
    • assertThrows: takes two arguments, the first being a class, and the second being a lambda expression.
  4. You can get the class of an exception by using Exception.class, for example, ArithmeticException.class or java.lang.ArithmeticException.class if you’ve not imported it.

  5. The lambda expression should include the method call that should throw an exception. You can think of it as a small convenient try block, for example:

    assertThrows(ArithmeticException.class, () -> c1.divide());
    // or if you need multiple statements
    assertThrows(ArithmeticException.class, () -> {
        Calculator c1 = new Calculator(1, 2);
        c1.divide();
    });
  6. See Assertions (JUnit 5.0.1 API) for documentation on all assertions. You might be able to find something that reduces the amount of code you need to write!

  7. We are using JUnit 5, and the way you import these assertions for JUnit 5 is:

    import static org.junit.jupiter.api.Assertions.*;
  8. Note: Make sure you’ve added package Calculator; to the top of the Calculator.CalculatorTest class file.

  9. Every method that is meant to be a test needs to be marked with a @Test decorator. This helps our tool find and identify which methods are intended to be tests. You can import this decorator by saying:

    import org.junit.jupiter.api.Test;
  10. Later, you can mark a method as a test by saying:

    @Test
    public void testAdd() {
        // body
    }
  11. You might also want to import java.lang.ArithmeticException, that is the exception thrown by the divide method.

  12. Next, create a class as usual:

    public class CalculatorTest {}
  13. Inside this class, define a bunch of calculator objects. Here are some you might find useful:

    public class CalculatorTest { 
        /*
        * Here is where you want to define a bunch of calculator objects   
        * So that you can use them later in your tests.   
        *   
        * The instructions document contains a list of calculator objects   
        * that might be useful to test.   
        *   
        * You can also create your own calculator objects. 
        */
    
        Calculator c1 = new Calculator(2, 3);
        Calculator c2 = new Calculator(-2, 3);
        Calculator c3 = new Calculator(2, -3);
        Calculator c4 = new Calculator(-2, -3);
        Calculator c5 = new Calculator(0, 0);
        Calculator c6 = new Calculator(0, 3);
        Calculator c7 = new Calculator(2, 0);
        Calculator c8 = new Calculator(0, -3);
        Calculator c9 = new Calculator(-2, 0);
    }
  14. Next, use the @Test decorator just like how it was described above to create:

    • testAdd()
    • testSubtract()
    • testMultiply()
    • testDivide()
  15. Implement all the above tests, and run them to make sure they’re all passing.

  16. Once your tests are passing, use the above instructions to check your grade within the IDE.

  17. Make sure you modify your tests or source code to get rid of all surviving mutations. Once you are done, hit submit.

7.4 Rubric

  • Unit tests for Calculator.Calculator should pass (10 points)
  • There should be no surviving mutants. Points are only awarded for no surviving mutations (40 points)

7.5 Project Files

Download the project files here.

7.6 Opening Project in Visual Studio Code

  1. Download the project files from

  2. Unzip the files to preferably an ITSC 2214 folder for this class. You must have created this folder before for previous labs. Unzip_Location_ITSC_2214

  3. Launch Visual Studio Code. Go to File > Open Folder…, navigate and select the folder where you have extracted the zip file. Vs_code_extract_zip

7.7 Update the autograder

First, please update your autograder by running:

umm update

7.8 Checking Autograder Feedback

You can check your grade locally by following these steps:

  1. Open a terminal. To open a terminal in Visual Studio Code on different operating systems:

    • Windows: Press ” Ctrl + ` ” or ” Ctrl + Shift + ` ” to open the integrated terminal.

    • Mac: Press ” Cmd + ` ” or ” Cmd + Shift + ` ” to open the integrated terminal.

  2. Run the command:

    umm grade ./script.rhai

7.9 Submitting your Project

When you are ready to submit your assignment:

  1. Open a terminal. To open a terminal in Visual Studio Code on different operating systems:

    • Windows: Press ” Ctrl + ` ” or ” Ctrl + Shift + ` ” to open the integrated terminal.

    • Mac: Press ” Cmd + ` ” or ” Cmd + Shift + ` ” to open the integrated terminal.

  2. You can copy and run the umm create-submission command in the terminal, and that should create a zip file with a name similar to submission-2024-01-24-15-04-50.zip.

    umm create-submission
  3. Submit the submission-2024-... .zip file to Gradescope. The submission zip file will appear in the file explorer tab of VS Code. You can right click on this file and click on reveal in explorer (windows) or reveal in finder (mac) in order to find this file. then, you can drag and drop this to gradescope for submission.