Search code examples
junitjunit5

Migrate Junit4 to Junit5 test cases


I have this JUnit4 test which I would like to migrate to JUnit5:

import org.junit.runner.RunWith;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class TasksTest {

  @Test(expected = Exception.class)
  public void createTaskTest() {
    .....
  }
}

What should be proper wya to migrate this?

import org.junit.runner.RunWith;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class TasksTest {

  @Test(expected = Exception.class)
  public void createTaskTest() {
    .....
  }
}

But I get these errors: Incompatible types. Found: 'java.lang.Class<org.mockito.junit.MockitoJUnitRunner.StrictStubs>', required: 'java.lang.Class<? extends org.junit.jupiter.api.extension

and

Cannot resolve method 'expected'

Can you advise how this can be fixed?


Solution

  • Migration tips from JUnit guide suggest to use Assertions.assertThrows:

    @Test
    public void createTaskTest() throws Exception {
        Assertions.assertThrows(Exception.class, () -> {
            //.....
        });
    }
    

    Regarding "Migrate" refactoring in IDEA, there's a ticket to support expected and timeout: https://youtrack.jetbrains.com/issue/IDEA-230146, you can vote for it