From b52da64001da3a4458510228d267e6c4abd84252 Mon Sep 17 00:00:00 2001 From: Eric Gao Date: Wed, 21 Sep 2022 10:15:21 +0800 Subject: [PATCH] [Improvement][UT] Upgrade jUnit to 5.+ (#10976) (#11332) * [Improvement][UT] Upgrade jUnit to 5.+ (#10976) * Refactor AlertServerTest with jUnit-5 as an example --- .../dolphinscheduler-alert-server/pom.xml | 2 +- .../alert/AlertServerTest.java | 50 +++++++++---------- dolphinscheduler-api/pom.xml | 9 ++-- .../TaskInstanceControllerTest.java | 12 +++-- pom.xml | 41 ++++++++++----- 5 files changed, 66 insertions(+), 48 deletions(-) diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml index 587619f9f..b044e9488 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml +++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml @@ -26,7 +26,6 @@ dolphinscheduler-alert-server jar ${project.artifactId} - @@ -158,4 +157,5 @@ + diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java index e739ff4d0..63381a261 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java @@ -17,57 +17,53 @@ package org.apache.dolphinscheduler.alert; -import junit.framework.TestCase; -import org.apache.dolphinscheduler.dao.AlertDao; import org.apache.dolphinscheduler.dao.PluginDao; -import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.remote.NettyRemotingServer; import org.apache.dolphinscheduler.remote.config.NettyServerConfig; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.powermock.reflect.Whitebox; -import java.util.ArrayList; -import java.util.List; +@ExtendWith(MockitoExtension.class) +public class AlertServerTest { - -@RunWith(MockitoJUnitRunner.class) -public class AlertServerTest extends TestCase { - - @InjectMocks - private AlertServer alertServer; - @Mock private PluginDao pluginDao; - + @Mock private AlertConfig alertConfig; @Mock private AlertSenderService alertSenderService; - - @Test - public void testStart() { - Mockito.when(pluginDao.checkPluginDefineTableExist()).thenReturn(true); + @InjectMocks + private AlertServer alertServer; + + @BeforeEach + void init() { + Mockito.lenient().when(pluginDao.checkPluginDefineTableExist()).thenReturn(true); - Mockito.when(alertConfig.getPort()).thenReturn(50052); + Mockito.lenient().when(alertConfig.getPort()).thenReturn(50052); Mockito.doNothing().when(alertSenderService).start(); + } + @Test + public void alertServerStartSuccessfully() { + alertServer.run(null); - + NettyRemotingServer nettyRemotingServer = Whitebox.getInternalState(alertServer, "nettyRemotingServer"); - + NettyServerConfig nettyServerConfig = Whitebox.getInternalState(nettyRemotingServer, "serverConfig"); - - Assert.assertEquals(50052, nettyServerConfig.getListenPort()); + + Assertions.assertEquals(50052, nettyServerConfig.getListenPort()); } } diff --git a/dolphinscheduler-api/pom.xml b/dolphinscheduler-api/pom.xml index 6b8fc764f..efaac0b7d 100644 --- a/dolphinscheduler-api/pom.xml +++ b/dolphinscheduler-api/pom.xml @@ -26,7 +26,6 @@ dolphinscheduler-api jar ${project.artifactId} - @@ -38,7 +37,6 @@ - @@ -158,6 +156,12 @@ test + + org.mockito + mockito-core + test + + org.apache.curator curator-test @@ -192,7 +196,6 @@ test - diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/TaskInstanceControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/TaskInstanceControllerTest.java index 7090a2f37..b73a21302 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/TaskInstanceControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/TaskInstanceControllerTest.java @@ -71,9 +71,11 @@ public class TaskInstanceControllerTest extends AbstractControllerTest { result.setMsg(Status.SUCCESS.getMsg()); when(taskInstanceService.queryTaskListPaging(any(), eq(1L), eq(1), eq(""), eq(""), eq(""), eq(""), any(), any(), - eq(""), Mockito.any(), eq("192.168.xx.xx"), eq(TaskExecuteType.BATCH), any(), any())).thenReturn(result); + eq(""), Mockito.any(), eq("192.168.xx.xx"), eq(TaskExecuteType.BATCH), any(), any())) + .thenReturn(result); Result taskResult = taskInstanceController.queryTaskListPaging(null, 1L, 1, "", "", "", - "", "", TaskExecutionStatus.SUCCESS, "192.168.xx.xx", "2020-01-01 00:00:00", "2020-01-02 00:00:00", TaskExecuteType.BATCH, pageNo, pageSize); + "", "", TaskExecutionStatus.SUCCESS, "192.168.xx.xx", "2020-01-01 00:00:00", "2020-01-02 00:00:00", + TaskExecuteType.BATCH, pageNo, pageSize); Assert.assertEquals(Integer.valueOf(Status.SUCCESS.getCode()), taskResult.getCode()); } @@ -91,9 +93,9 @@ public class TaskInstanceControllerTest extends AbstractControllerTest { MvcResult mvcResult = mockMvc.perform(post("/projects/{projectName}/task-instance/force-success", "cxc_1113") .header(SESSION_ID, sessionId) .params(paramsMap)) - .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andReturn(); + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andReturn(); Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); diff --git a/pom.xml b/pom.xml index 0efadbdb7..ef784156f 100644 --- a/pom.xml +++ b/pom.xml @@ -64,14 +64,15 @@ UTF-8 2.6.1 1.8 - 4.13.1 + 5.9.0 + 3.9.0 3.1.12 3.3 3.3.0 2.5.3 2.10.3 2.4 - 2.22.1 + 3.0.0-M6 3.1.1 3.2.1 2.2.0 @@ -309,6 +310,13 @@ dolphinscheduler-tools ${project.version} + + org.junit + junit-bom + ${junit.version} + pom + import + @@ -321,9 +329,25 @@ of the submodules. --> - junit - junit - ${junit.version} + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.vintage + junit-vintage-engine + test + + + org.mockito + mockito-core + ${mockito.version} + test + + + org.mockito + mockito-junit-jupiter + ${mockito.version} test @@ -581,13 +605,6 @@ ${project.build.directory}/jacoco.exec - - - org.apache.maven.surefire - surefire-junit4 - ${maven-surefire-plugin.version} - - -- GitLab