Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

New useful functionality will be added:


  • Check to throw exceptions

    :

Code Block
public void shouldRaiseAnException () throws Exception {
    Assertions.assertThrows(Exception.class, () -> {
            //...
    });
}

...

Code Block
@Test(expected = Exception.class)


  • Ability to check timeouts

    :

Code Block
@Test(timeout = 1)
public void shouldFailBecauseTimeout () throws InterruptedException {
    Thread.sleep(10);
}

@Test
public void shouldFailBecauseTimeout () throws InterruptedException {
    Assertions.assertTimeout(Duration.ofMillis(1), () -> Thread.sleep(10));
}


  • Compatibility with Java8

    ,

Now it is possible to write lambdas directly from Assert:

Code Block
@Test
public void shouldFailBecauseTheNumbersAreNotEqual_lazyEvaluation () {
    Assertions.assertTrue(
        2 == 3,
        () -> "Numbers " + 2 + " and " + 3 + " are not equal!");
}


@Test
public void shouldAssertAllTheGroup () {
    List<Integer> list = Arrays.asList(1, 2, 4);
    Assertions.assertAll("List is not incremental",
        () -> Assertions.assertEquals(list.get(0).intValue(), 1),
        () -> Assertions.assertEquals(list.get(1).intValue(), 2),
        () -> Assertions.assertEquals(list.get(2).intValue(), 3));
}

...


  • Assumptions

Tests will be executed under conditions:

Code Block
@Test
public void whenEnvironmentIsWeb_thenUrlsShouldStartWithHttp () {
    assumingThat("WEB".equals(System.getenv("ENV")),
        () -> {
            assertTrue("http".startsWith(address));
        });
}


  • Useful to check flaky tests

    :

Code Block
@RepeatedTest(value = 3, name = "Custom name {currentRepetition}/{totalRepetitions}")
void repeatedTestWithCustomDisplayName (TestInfo testInfo){
    assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}


  • Parameterized Test

    :

Code Block
@ParameterizedTest
@ValueSource(strings = {"Hello", "JUnit"})
void withValueSource (String word){
    assertNotNull(word);
}

...