Lecture 07 - 02/01

Testing

/** Tests the methods of the Sort class */
public class TestSort {
public static testSort() {
String[] input = {"cows", "dwell", "above", "clouds"};
String[] expected = {"above", "cows", "clouds", "dwell"};
Sort.sort(input);
org.junit.Assert.assertArrayEquals(expected, input);
}
public static void main(String[] args) {
testSort();
}
}

Example: Designing Selection Sort

Summary:

JUnit Tricks:

Instead of using:
org.junit.Assert.assertEquals(expected, actual);

We can instead use annotations:

  1. Annotate each test with @org.junit.Test
  2. Change all test methods to non-static
  3. Use a JUnit runner to run all tests and tabulate results
    1. IntelliJ provides a default runner/renderer. OK to delete main.
    2. Rendered output is easier to read, no need to manually invoke tests

Also, by doing:

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

We will no longer need to type org.junit or org.junit.Assert parts of JUnit testing commands (ex: @Test).