Description
JUnit is the work horse for testing in Java, so there’s plenty of material online. Here, I’m putting up the short-short version, mostly for comparison.
Setup
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
Functionality
Lifecycle
@BeforeClass
public static void setupOnce() {
System.err.println("setupOnce: Executed once per test class");
}
@Before
public void setup() {
System.err.println("setup: Executed for every test");
}
@Test
public void testMethod() {
System.err.println("Running test method");
}
@After
public void tearDown() {
System.err.println("tearDown: Executed for every test");
}
@AfterClass
public static void tearDownOnce() {
System.err.println("tearDown: Executed once per test class");
}</pre>
<pre>
Assertions
@Test
public void basicEquality() {
final int expected = 42;
assertEquals(expected, 42);
assertNotEquals(expected, 43);
}
@Test
public void collectionEquality() {
// Collections are equal if their members implement equals (which wrapper types do)
assertEquals(Arrays.asList(1, 2, 3), Arrays.asList(10 - 9, 10 / 5, 10 % 7));
}
@Test
public void arrayEquality() {
final int[] expected = new int[]{1, 2, 3};
assertArrayEquals(expected, new int[] {2 - 1, 6 % 4, 3});
}
@Test
public void decimalEquality() {
final double expectedDecimal = 42.0;
final double precision = 0.1;
// Equal because of precision
assertEquals(expectedDecimal, 42.01, precision);
}
@Test
public void truth() {
assertTrue(true);
assertFalse(false);
}
@Test
public void nulls() {
assertNull(null);
assertNotNull("null");
}
@Test
public void unconditionalFailure() {
fail("This test will fail");
}
Exceptions
@Rule
public ExpectedException exception = ExpectedException.none();
@Test(expected = IllegalArgumentException.class)
public void usingAnnotation() {
throw new IllegalArgumentException("Fail!");
}
@Test
public void usingRule() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Epic");
exception.expectCause(isA(IllegalStateException.class));
throw new IllegalArgumentException("Epic", new IllegalStateException("Fail"));
}
Parameterized tests
@RunWith(Parameterized.class)
public class ParameterizedConstructorTest {
private String firstWord;
private String secondWord;
private String concatenated;
@Parameters(name = "Case {index}: '{0}' concatenated with '{1}'")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{"Hello", "World", "Hello World"},
{"You", "Me", "You Me"}
});
}
public ParameterizedConstructorTest(String firstWord, String secondWord, String concatenated) {
this.firstWord = firstWord;
this.secondWord = secondWord;
this.concatenated = concatenated;
}
@Test
public void usingConstructor() {
assertEquals(concatenated, firstWord + " " + secondWord);
}
}
Injection-based version
@RunWith(Parameterized.class)
public class ParameterizedInjectionTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{"Hello", "World", "Hello World"},
{"You", "Me", "You Me"}
});
}
@Parameter
public String firstWord;
@Parameter(value = 1)
public String secondWord;
@Parameter(value = 2)
public String concatenated;
@Test
public void usingInjection() {
assertEquals(concatenated, firstWord + " " + secondWord);
}
}
Theory tests
@RunWith(Theories.class)
public class TheoryTest {
// Data points can be supplied using methods
@DataPoints
public static int[] count() {
return new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
@DataPoints
public static String[] animal = new String[]{"cat", "dog", "bird"};
@Theory
public void animalsArePluralized(int count, String animal) {
assumeTrue(count > 1); // Discard 1s to get nice plurals
assertTrue(pluralize(count, animal).matches("^\\d{1,2} \\w+s$"));
}
public static String pluralize(int count, String s) {
return String.format("%d %ss", count, s);
}
}