8  7L. Writing JUnit Tests - 2

8.1 Project Overview

Note: You will be creating this project structure from scratch. These directories and files will not be provided to you.

.
├── src
│   └── UnitTesting
│       ├── CoOrds.java
│       └── Student.java
└── test
    └── UnitTesting
        ├── CoOrdsTest.java 
        └── StudentTest.java

8.2 Detailed Instructions

In this lab, we are going to write tests for a Comparable interface implementation and learn how to structure projects.

8.2.1 Steps to Follow

  1. Create Source Folder
    • Create a folder called src. This folder will contain our source files.
  2. Create Package Folder
    • Inside src, create a folder called UnitTesting. This represents a package called UnitTesting. It helps to keep test classes and source classes in the same package.
  3. Create CoOrds Class
    • Inside the UnitTesting package, create a source class called CoOrds.java and copy the following code into it:
    package UnitTesting;
    
    public class CoOrds implements Comparable<CoOrds> {
        int x, y;
    
        public CoOrds() {
            x = y = 0;
        }
    
        public CoOrds(int X, int Y) {
            x = X;
            y = Y;
        }
    
        public int compareTo(CoOrds other) {
            if (x != other.x) {
                return x - other.x;
            } else {
                return y - other.y;
            }
        }
    }
  4. Create Student Class
    • Also inside the UnitTesting package, create another source class called Student.java and copy the following code into it:
    package UnitTesting;
    
    public class Student implements Comparable<Student> {
        String name;
        char grade;
    
        public Student() {
        }
    
        public Student(String n, char g) {
            name = n;
            grade = g;
        }
    
        public int compareTo(Student other) {
            if (grade == other.grade) {
                if (name.charAt(0) == other.name.charAt(0)) {
                    return 0;
                } else {
                    return name.charAt(0) - other.name.charAt(0);
                }
            } else {
                return other.grade - grade;
            }
        }
    }
  5. Create Test Folder
    • Create a folder called test. This folder will contain our test files.
  6. Create Test Package Folder
    • Inside test, create a folder called UnitTesting. This represents a package called UnitTesting.
  7. Create Test Classes
    • Inside the UnitTesting test package, create two classes called CoOrdsTest.java and StudentTest.java. > You can copy your CalculatorTest contents from a previous lab here and then modify them to contain tests for CoOrds and Student, respectively. This is not required, but it will save you time by providing a starting point.

8.2.2 Project Structure

Ensure each class in this project belongs to the UnitTesting package. Your final project structure should look like this:

.
├── src
│   └── UnitTesting
│       ├── CoOrds.java
│       └── Student.java
└── test
    └── UnitTesting
        ├── CoOrdsTest.java
        └── StudentTest.java

8.2.3 Final Steps

  1. Run and Pass Tests
    • Once your tests are passing, you can run the grading command to perform mutation testing.
  2. Mutation Testing
    • Remember, you cannot run mutation tests via the autograder if your tests aren’t passing. Mutation testing only works on passing tests.

8.3 Rubric

  • Project must compile, otherwise no grade.
  • Complete JavaDoc for UnitTesting.CoOrds (5 points)
  • Complete JavaDoc for UnitTesting.Student (5 points)
  • No surviving mutants for UnitTesting.CoOrdsTest (20 points)
  • No surviving mutants for UnitTesting.StudentTest (20 points)

Submissions will be manually reviewed by TAs. Your grade may change as a result.

8.4 Project Files

Download the project files here.

8.5 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

8.6 Update the autograder

First, please update your autograder by running:

umm update

8.7 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

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