-
Notifications
You must be signed in to change notification settings - Fork 5k
Description
Search before asking
- I had searched in the issues and found no similar feature requirement.
Description
-
This is part of DSIP-10 [DSIP-10][Unit Tests] Improve DolphinScheduler unit tests #10573
-
By this moment, we have added jUnit 5 dependencies [Improvement][UT] Upgrade jUnit to 5.+ (#10976) #11332 and removed all
Powermockrelated code [Improvement][Test] Remove dependency of powermock #11405. It is high time that we migrate all the UT cases from jUnit 4 to jUnit 5 and remove the dependency ofjunit-vintage-engineso that there will be one and only one version of unit test cases, which is jUnit5, in the whole project.
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:
- https://blogs.oracle.com/javamagazine/post/migrating-from-junit-4-to-junit-5-important-differences-and-benefits
- https://blog.jetbrains.com/idea/2020/08/migrating-from-junit-4-to-junit-5/
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.
- Perform basic migration through
Intellij IDEfor the module you choose.

- Replace
@RunWith(MockitoJUnitRunner.class)with@ExtendWith(MockitoExtension.class)if necessary. - Replace
AssertwithAssertions. - 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, () -> {
//...
});
}- 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), () -> {});- Replace
@Ignorewith@Disableif necessary. - Replace
org.junit.rules.TemporaryFolderwithorg.junit.jupiter.api.io.TempDirand use@TempDirif necessary. - Run the UT file you changed to see whether all cases pass successfully.
- Fix unnecessary stubbings if necessary. This is caused by changing
@Beforeinto@BeforeEach. The recommend method is to use@MockitoSettings(strictness = Strictness.LENIENT). Alternatively, you could either remove the unnecessary stubbings from@BeforeEachand only put them where they needed or useMockito.lenient().when(......forMockito.when(......to bypass thestubbing check. - Global search in your module and make sure all
org.junit.xxxxhas been migrated toorg.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.
- Migrate all UT cases from jUnit 4 to jUnit 5 in
task-pluginmodule: [Improvement][Test] Migrate all UT cases from jUnit4 to jUnit5 in task-plugin module as an example #12299 - Migrate all UT cases from jUnit 4 to jUnit 5 in
alertandapimodule: [Migration][Test] Migrate all UT cases from jUnit 4 to jUnit 5 in alert and api module #12313 - Migrate all UT cases from jUnit 4 to jUnit 5 in
daomodule: Migrate all UT cases from jUnit 4 to jUnit 5 in dao module #12319 - Migrate all UT cases from jUnit 4 to jUnit 5 in
data-quality,datasource-pluginandregistrymodule: [Migrate][Test] Migrate all UT cases from jUnit 4 to jUnit 5 in data-quality, datasource-plugin and registry module #12322 - Migrate all UT cases from jUnit 4 to jUnit 5 in
master,workerandremotemodule: [Migration][Test] Migrate all UT cases from jUnit 4 to jUnit 5 in master, worker and remote module #12316 - Migrate all UT cases from jUnit 4 to jUnit 5 in
common,serviceandspimodule: [Migrate ][Test] Migrate all UT cases from jUnit 4 to jUnit 5 in common, service and spi module #12317 - Migrate all UT cases from jUnit 4 to jUnit 5 in
microbenchande2emodule: [Migration][Test] Migrate all UT cases from jUnit 4 to jUnit 5 in microbench and e2e module #12333 - Block explicit imports of jUnit 4 library. [Migrate][Test] Block explicit import of jUnit 4 library #12395
Are you willing to submit a PR?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct