JUnit 5 - Jupiter
Neue Assertions
assertTrue(service.isEnabled(), "Expected to be true");
assertEquals(expected, actual, () -> "Expected value didn't match");
assertAll("Addresstests",
() -> assertEquals("Nürnberg", addresse.getOrt()),
() -> assertEquals("90425", addresse.getPlz())
);
Exception ex = assertThrows(Exception.class, service::invoke);
assertTimeout(Duration.parse("PT0.500S"), service::invoke);
assertTimeoutPreemptivly(
Duration.parse("PT0.500S"), service::invoke);
JUnit 5 - Jupiter
Neue Assumptions
assumeTrue(service.isEnabled(), "Service should be enabled!");
assumeFalse(service.isEnabled(), () -> "That didn‘t work!");
assumingThat(service.isEnabled(), () -> { /* do something */ };
JUnit 5 - Jupiter
Verschachtelte Tests
class TestWrapper {
@BeforeEach
void setup() { /* .... */ }
@Nested
class WrappedTest1 {
@Test
void test() { /* ... */ }
}
@Nested
class WrappedTest2 {
@Test
void test() { /* ... */ }
}
}
JUnit 5 - Jupiter
Benannte Tests
@DisplayName("Eine Adresse sollte")
class AddressTests {
@Test
@DisplayName("einen gültigen Ort besitzen")
void shouldHaveValidCity() {
assertNotNull(adresse.getOrt());
}
@Test
@DisplayName("eine gültige Postleitzahl besitzen")
void shouldHaveValidCity() {
assertNotNull(adresse.getOrt());
}
}
JUnit 5 - Jupiter
Wie war das bei JUnit4?
@RunWith(Parameterized.class)
public class FibonacciTest {
@Parameters public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{0,0},{1,1},{2,1},{3,2} })};
private int input, expected;
public FibonacciTest(int input, int expected) {
this.input = input; this.expected = expected;
}
@Test
public void test() {
assertEquals(expected, Fibonacci.compute(input));
}
}
JUnit 5 - Jupiter
Dynamische Tests:
class DynamicTests {
@TestFactory
List<DynamicTest> createSomeTests() {
return Arrays.asList(
DynamicTest.dynamicTest("First dynamically created test",
() -> assertEquals(/* ... */)),
DynamicTest.dynamicTest("Second dynamically created test",
() -> assertEquals(/* ... */))
);
}
}
JUnit 5 - Jupiter
class ParameterizedTests {
@ParameterizedTest
@ValueSource(ints = {1,2,3,4,5})
void valueSourceTest(int param){ /* ... */ }
@ParameterizedTest
@CsvFileSource(resources = "/file.csv")
void csvSourceTest(String token1, String token2){ /* ... */ }
@ParameterizedTest
@MethodSource("paramMethod")
void methodSourceTest(String param){ /* ... */ }
static Iterable<String> paramMethod() { /* ... */ }
}
JUnit 5 - Jupiter
class RepeatableTests {
@RepeatableTest(10)
void repeatableTest(RepetitionInfo repetitionInfo) {
/* ... */
}
}
JUnit 5 - Jupiter
Tests können deaktiviert werden:
class DisabledTests {
@Test
@Disabled
void breakingTest() {
fail("This never works!");
}
@Test
@DisabledOnWeekends
void breaksOnWeekends() {
fail("Meeh, it's weekend again!");
}
}
JUnit 5 - Jupiter
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.*;
class DisabledOnWeekendsCondition implements TestExecutionCondition {
@Override
public ConditionEvaluationResult evaluate(TestExtensionContext context) {
if (isWeekend())
return disabled("Should not be run on weekends!");
else
return enabled("Should be run on weekdays!");
}
// ...
}