JUnit 4 cheat sheet

Shortcuts

Commons

    assertTrue("The assertion has failed", instance.isCorrect());
    @Before
    public void setup() {
        // do something before each unit test runs
    }

    @After
    public void teardown() {
       // do something each each unit test ran
    }
    @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
    } 
    @Test(expected=NullPointerException.class)
    public void testSortClassNPE() {
        int[] numbers = null;
        Arrays.sort(numbers);
    }
    @Test(timeout=100)
    public void testPerformance() {
    } 
    @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));
        } 
    }
    @RunWith(Suite.class)
    @SuiteClasses({ Test1.class, Test2.class })
    public class TestSuiteOne {
    
    }