Search code examples
junitjunit5

Cannot resolve method 'assertNotThrows' in 'Assertions' in Junit5


I have this JUnit4 code which I want to migrate:

  @Test(expected = Test.None.class)
  public void test() {
  .....
  }

I tried to migrate this code to Junit5 this way:

import static org.junit.jupiter.api.Assertions;

Assertions.assertDoesNotThrow(ExceptionNotToThrow.class, () -> {
  .....
});

But I get:

Cannot resolve method 'assertDoesNotThrow(Class<ExceptionNotToThrow>, <lambda expression>)'

Do you know how I can migrate this code properly?


Solution

  • Just use assertDoesNotThrow without exception class:

    @Test
    void checkCodeDoesNotThrowAnyException() {
        Assertions.assertDoesNotThrow(() -> {
            //do something
        });
    }