Shortcuts
Commons
- For any assertion it is possible to write a description. For instance,
assertTrue("The assertion has failed", instance.isCorrect());
- To run a method before and after each test, can use
@Before
and@After
annotations,
@Before
public void setup() {
// do something before each unit test runs
}
@After
public void teardown() {
// do something each each unit test ran
}
- To limit running
@Before
and@After
to the entire class, we can use@BeforeClass
and@AfterClass
which execute one time when a unit test class running.
@BeforeClass
public static void setup() {
// do something before the unit test class runs
}
@AfterClass
public static void teardown() {
// do something after the unit test class ran
}
-
For comparing two arrays, use
assertArrayEquals
. -
Testing an exception in Junit,
@Test(expected=NullPointerException.class)
public void testSortClassNPE() {
int[] numbers = null;
Arrays.sort(numbers);
}
- Adding performance test on Junit tests,
@Test(timeout=100)
public void testPerformance() {
}
- Creating parameterized tests,
@RunWith(Parameterized.class)
public class TestClass {
private String input;
private String output;
public TestClass(String input, String output) {
this.input = input;
this.output = output;
}
@Parameters
public Collection<String[]> testConditions() {
String[][] expectedOutputs = { {"Input1", "Output1"}, {"Input2", "Output2"} }
return Arrays.asList(expectedOutputs);
}
@Test
public void testDoWhatever() {
assertEquals(output, myClass.doSomething(input));
}
}
-
It is not possible to have two parameterized tests in a single class, it must be a separate class.
-
Creating test suits,
@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class })
public class TestSuiteOne {
}