Skip to content

[Migration][Test] jUnit4 -> jUnit5 UT cases migration #12301

@EricGao888

Description

@EricGao888

Search before asking

  • I had searched in the issues and found no similar feature requirement.

Description

SOP

These two following articles are recommended for you to understand the whole process. In order to keep the instructions brief and concise, I summarize some key points of these two articles as well as add a few extra operations based on practice for your reference:

BTW, I suggest you perform this SOP below by module or by file, instead of doing it for the whole project. This helps you increase efficiency.

  1. Perform basic migration through Intellij IDE for the module you choose.
    screenshot1
  2. Replace @RunWith(MockitoJUnitRunner.class) with @ExtendWith(MockitoExtension.class) if necessary.
  3. Replace Assert with Assertions.
  4. Fix expected exception if necessary.
// jUnit 4
@Test(expected = Exception.class)
public void testThrowsException() throws Exception {
    // ...
}

// jUnit 5
@Test
public void testThrowsException() throws Exception {
    Assertions.assertThrows(Exception.class, () -> {
        //...
    });
}
  1. Fix timeout if necessary.
// jUnit 4
@Test(timeout = 10)
public void testFailWithTimeout() throws InterruptedException {
    Thread.sleep(100);
}

// jUnit 5
@Test
void testFailWithTimeout() throws InterruptedException {
    Assertions.assertTimeout(Duration.ofMillis(10), () -> Thread.sleep(100));
}

Assertions.assertTimeout(Duration.ofMillis(60000), () -> {});
  1. Replace @Ignore with @Disable if necessary.
  2. Replace org.junit.rules.TemporaryFolder with org.junit.jupiter.api.io.TempDir and use @TempDir if necessary.
  3. Run the UT file you changed to see whether all cases pass successfully.
  4. Fix unnecessary stubbings if necessary. This is caused by changing @Before into @BeforeEach. The recommend method is to use @MockitoSettings(strictness = Strictness.LENIENT). Alternatively, you could either remove the unnecessary stubbings from @BeforeEach and only put them where they needed or use Mockito.lenient().when(...... for Mockito.when(...... to bypass the stubbing check.
  5. Global search in your module and make sure all org.junit.xxxx has been migrated to org.junit.jupiter.api.xxx

PR Example

Sub-Tasks

We hope to complete all the following sub-tasks before Oct. 25th because DSIP-10 depends on this issue and all UT cases are supposed to be migrated to jUnit 5 before the start of UT refactoring.

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions