Testing

JUnit5BasicsTest

Path
pkg14testing/src/test/java/pkg14testing/JUnit5BasicsTest.java
Package
pkg14testing
Study order
5
Run
Read this file. It has no standalone main method.

There is no in-browser runner. This is the file from the curriculum, unchanged.

pkg14testing/src/test/java/pkg14testing/JUnit5BasicsTest.java
1package pkg14testing;2 3import org.junit.jupiter.api.*;4 5import static org.junit.jupiter.api.Assertions.*;6 7class JUnit5BasicsTest {8 9    @BeforeAll static void beforeAll() { System.out.println("  [JUnit] @BeforeAll"); }10    @AfterAll static void afterAll() { System.out.println("  [JUnit] @AfterAll"); }11 12    @BeforeEach void beforeEach() { System.out.println("  [JUnit] @BeforeEach"); }13    @AfterEach void afterEach() { System.out.println("  [JUnit] @AfterEach"); }14 15    @Test16    @DisplayName("addition works")17    void testAdd() {18        Calculator calc = new Calculator();19        assertEquals(4, calc.add(2, 2));20        assertTrue(calc.add(1, 1) > 1);21    }22 23    @Test24  void divideByZeroThrows() {25        Calculator calc = new Calculator();26        assertThrows(IllegalArgumentException.class, () -> calc.divide(1, 0));27    }28 29    @Test30    @Disabled("example disabled test")31    void skipped() { }32}