Unit Testing Level I

An introduction to using the JUnit test system.

Start testing little pieces of your code so it is not so hard for you to figure out what goes wrong. In Racket, we used the check-expect system. In Java there is a similar system called JUnit. This page explains how to use the JUnit 4 system that is built into BlueJ.

Basic Testing

(Include the boilerplate code from the next section!)

  • @Test
  • assertEquals(correct, actual)
  • assertEquals(correct, actual, close_enough): Like check-within, this is for comparing floating point values (double). Test succeeds if |correct - actual| < close_enough.
  • assertArrayEquals(correct, actual): Compare arrays.

Example:

@Test
public void test_math() {
    assertEquals(5, 2+3);
    assertEquals(.66, 2.0/3, 0.01);
}

Example for arrays (Chapter 12):

@Test
public void test_always_ok() {
    int[] correct = {1,2,3};
    int[] actual = {1,2,3};
    assertArrayEquals(correct, actual);
}

Example for ArrayList (Chapter 13):

@Test
public void test_arraylist() {
  ArrayList<Integer> n = new ArrayList<>();
  n.add(5); n.add(25); n.add(100);

  List<Integer> correct = Arrays.asList(5,25,100);
   
  assertEquals(correct, n);
} 

Testing Boilerplate

At the start of your code, include the following boilerplate:

import org.junit.*;
import org.junit.runner.*;
import static org.junit.Assert.*;

BlueJ

“New Class…” -> ClassType is Unit Test. Give it a name and make it. Box should be green.

See the sample code on GitHub.

Once you expose the “View -> Show Team and Test Controls”, there is a button to run tests.

Repl.it

  1. You should be using “Project Mode” and have multiple files open with names like “HospitalTest.java”. Capital letters matter, so match the name of the class.

  2. There is no “Test Project” button on Repl.it, but you can arrange for the main method to run your tests by using the code fragment below. (See the working main method.)

     org.junit.runner.JUnitCore.main("Test_Class_Name_MUST_CHANGE");
    
  3. Output should look like ..E.E...E.E.E.E....E, where the . indicates the test has begun running and the E indicates that it ended in an error. Notice there is no separate indication for a test passing.

Last modified August 18, 2023: 2022-2023 End State (7352e87)