Unit Testing


Does it work?

We all know that testing is an important process in making software. Unit testing is the process of testing each individual class, making sure all methods and functionalities are correct. Unit testing allows for errors in code to be identified quickly, before it is used in other classes where the error will spread. But it can be so infuriating and time consuming when you need to rerun the program and input test cases dozens of times to fix a single mistake. JUnit fixes this issue. JUnit allows for specific classes and its methods to be tested automatically. JUnit can be installed by going to their website and downloading the zip files they provide.


Create JUnit Testing Class

If you use JGrasp, making a JUnit test file is as easy as selecting Tools menu, clicking on JUnit, and lastly selecting create test file. This will automatically create a test file for the Java class you are currently viewing. If you aren't using JGrasp, and instead are using a IDE such as IntelliJ, you can copy the following template bellow.

class
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
    
public class ClassTest {
    /** Fixture initialization (common initialization for all tests). **/
    // insert fields for the test here (usually an object of class being tested)
    @Before public void setUp() { // required in every JUnit test, sets up fields
        // insert set up fields here
    }
    
    @Test public void methodTest() { // a single JUnit test, add more tests by using sane method declaration with different names
        // assertion statements to test class
    }
}

Assertions

Assertions state the expected behavior of a segment of code, and checks if the expected behavior happens. If it doesn't happen, then JUnit will inform the user of the fault that happened. We use assertions in our test cases to verify that our methods for our classes are preforming as expected. Assertions are written in code as "Assert.assertionName()" where assetionName is one of the various assert methods. The most common statements are Assert.assertEquals(expected, actual), Assert.assertTrue(bool), and Assert.assetFalse(bool). Each of these statements pass if their respective names are true. If otherwise, the entire test case fails. This is why it is important to make multiple testing methods for each individual method. It helps narrow down where mistakes exist.

Find a full list of assertions here.
error

A assertion that fails looks like this


pass

A assertion that passes looks like this


Example

public class Point{ // a point on a cartesian plane
    // fields
    protected int x; // the location on the x axis
    protected int y; // the location on the y axis
    // methods
    Point(int x, int y){
        this.x = x;  
        this.y = y; 
    }
    int dist(){ // manhattan distance
        return x+y;
    }
}
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
    
public class PointTest {
    Point p; // make fields
    @Before public void setUp() {
        p = new Point(4,5); //set up fields
    }
    
    @Test public void distTest() { // test
        Assert.assertEquals(9, p.dist()); //dist should be 9
    }
}