From cc82206c33cc60cd71ebfe7b327e8cc4110bd15d Mon Sep 17 00:00:00 2001 From: Eric Gao Date: Mon, 26 Sep 2022 17:02:14 +0800 Subject: [PATCH] Remove powermock in dolphinscheduler-master module (#12143) * Remove powermock related code in dolphinscheduler-master module * Remove redundant comments, useless code and unnecessary stubbings --- dolphinscheduler-master/pom.xml | 8 +++ .../server/master/SubProcessTaskTest.java | 63 +++++++------------ .../master/processor/CacheProcessorTest.java | 5 +- .../processor/TaskAckProcessorTest.java | 35 +++-------- .../TaskKillResponseProcessorTest.java | 4 +- .../registry/MasterRegistryClientTest.java | 24 +------ .../runner/MasterTaskExecThreadTest.java | 44 ++----------- .../runner/WorkflowExecuteRunnableTest.java | 49 ++++----------- .../master/service/FailoverServiceTest.java | 22 +------ 9 files changed, 63 insertions(+), 191 deletions(-) diff --git a/dolphinscheduler-master/pom.xml b/dolphinscheduler-master/pom.xml index 9294794a76..03084f91de 100644 --- a/dolphinscheduler-master/pom.xml +++ b/dolphinscheduler-master/pom.xml @@ -277,6 +277,14 @@ spring-cloud-starter-kubernetes-fabric8-config + + org.mockito + mockito-inline + 3.12.4 + + test + + diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/SubProcessTaskTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/SubProcessTaskTest.java index 27357d3c05..a69435a576 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/SubProcessTaskTest.java +++ b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/SubProcessTaskTest.java @@ -18,11 +18,10 @@ package org.apache.dolphinscheduler.server.master; import org.apache.dolphinscheduler.common.enums.TimeoutFlag; -import org.apache.dolphinscheduler.common.lifecycle.ServerLifeCycleManager; import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus; +import org.apache.dolphinscheduler.common.lifecycle.ServerLifeCycleManager; import org.apache.dolphinscheduler.common.model.TaskNode; import org.apache.dolphinscheduler.common.utils.JSONUtils; -import org.apache.dolphinscheduler.dao.AlertDao; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.TaskDefinition; import org.apache.dolphinscheduler.dao.entity.TaskInstance; @@ -35,22 +34,22 @@ import org.apache.dolphinscheduler.server.master.runner.task.SubTaskProcessor; import org.apache.dolphinscheduler.server.master.runner.task.TaskAction; import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; import org.apache.dolphinscheduler.service.process.ProcessService; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.springframework.context.ApplicationContext; -import java.time.Duration; -import java.util.ArrayList; -import java.util.List; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({ServerLifeCycleManager.class}) +@RunWith(MockitoJUnitRunner.class) public class SubProcessTaskTest { /** @@ -62,6 +61,8 @@ public class SubProcessTaskTest { private ProcessInstance processInstance; + private MockedStatic mockedStaticServerLifeCycleManager; + @Before public void before() { ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); @@ -73,38 +74,26 @@ public class SubProcessTaskTest { config.setTaskCommitRetryTimes(3); config.setTaskCommitInterval(Duration.ofSeconds(1)); - PowerMockito.mockStatic(ServerLifeCycleManager.class); - PowerMockito.when(ServerLifeCycleManager.isStopped()).thenReturn(false); + mockedStaticServerLifeCycleManager = Mockito.mockStatic(ServerLifeCycleManager.class); + Mockito.when(ServerLifeCycleManager.isStopped()).thenReturn(false); processService = Mockito.mock(ProcessService.class); Mockito.when(applicationContext.getBean(ProcessService.class)).thenReturn(processService); - AlertDao alertDao = Mockito.mock(AlertDao.class); - Mockito.when(applicationContext.getBean(AlertDao.class)).thenReturn(alertDao); - processInstance = getProcessInstance(); - TaskInstance taskInstance = getTaskInstance(); - - Mockito.when(processService - .findProcessInstanceById(processInstance.getId())) - .thenReturn(processInstance); - - // for SubProcessTaskExecThread.setTaskInstanceState Mockito.when(processService .updateTaskInstance(Mockito.any())) .thenReturn(true); - // for MasterBaseTaskExecThread.submit - Mockito.when(processService - .submitTask(processInstance, taskInstance)) - .thenAnswer(t -> t.getArgument(0)); - TaskDefinition taskDefinition = new TaskDefinition(); taskDefinition.setTimeoutFlag(TimeoutFlag.OPEN); taskDefinition.setTimeoutNotifyStrategy(TaskTimeoutStrategy.WARN); taskDefinition.setTimeout(0); - Mockito.when(processService.findTaskDefinition(1L, 1)) - .thenReturn(taskDefinition); + } + + @After + public void after() { + mockedStaticServerLifeCycleManager.close(); } private TaskInstance testBasicInit(WorkflowExecutionStatus expectResult) { @@ -112,10 +101,6 @@ public class SubProcessTaskTest { ProcessInstance subProcessInstance = getSubProcessInstance(expectResult); subProcessInstance.setVarPool(getProperty()); - // for SubProcessTaskExecThread.waitTaskQuit - Mockito.when(processService - .findProcessInstanceById(subProcessInstance.getId())) - .thenReturn(subProcessInstance); Mockito.when(processService .findSubProcessInstance(processInstance.getId(), taskInstance.getId())) .thenReturn(subProcessInstance); @@ -125,10 +110,7 @@ public class SubProcessTaskTest { @Test public void testBasicSuccess() { - TaskInstance taskInstance = testBasicInit(WorkflowExecutionStatus.SUCCESS); - // SubProcessTaskExecThread taskExecThread = new SubProcessTaskExecThread(taskInstance); - // taskExecThread.call(); - // Assert.assertEquals(ExecutionStatus.SUCCESS, taskExecThread.getTaskInstance().getState()); + testBasicInit(WorkflowExecutionStatus.SUCCESS); } @Test @@ -161,10 +143,7 @@ public class SubProcessTaskTest { @Test public void testBasicFailure() { - TaskInstance taskInstance = testBasicInit(WorkflowExecutionStatus.FAILURE); - // SubProcessTaskExecThread taskExecThread = new SubProcessTaskExecThread(taskInstance); - // taskExecThread.call(); - // Assert.assertEquals(ExecutionStatus.FAILURE, taskExecThread.getTaskInstance().getState()); + testBasicInit(WorkflowExecutionStatus.FAILURE); } private TaskNode getTaskNode() { diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/CacheProcessorTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/CacheProcessorTest.java index 20befa0f1c..0d8140bf23 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/CacheProcessorTest.java +++ b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/CacheProcessorTest.java @@ -28,7 +28,7 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; @@ -37,8 +37,9 @@ import io.netty.channel.Channel; /** * task ack processor test */ -@RunWith(PowerMockRunner.class) +@RunWith(MockitoJUnitRunner.class) public class CacheProcessorTest { + @InjectMocks private CacheProcessor cacheProcessor = new CacheProcessor(); diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java index 225e41aeba..6b4b8811d9 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java +++ b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java @@ -29,17 +29,15 @@ import java.util.Date; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; import io.netty.channel.Channel; /** * task ack processor test */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({SpringApplicationContext.class, TaskEvent.class}) +@RunWith(MockitoJUnitRunner.class) public class TaskAckProcessorTest { private TaskExecuteRunningProcessor taskExecuteRunningProcessor; @@ -51,18 +49,18 @@ public class TaskAckProcessorTest { @Before public void before() { - PowerMockito.mockStatic(SpringApplicationContext.class); + Mockito.mockStatic(SpringApplicationContext.class); - taskEventService = PowerMockito.mock(TaskEventService.class); - PowerMockito.when(SpringApplicationContext.getBean(TaskEventService.class)).thenReturn(taskEventService); + taskEventService = Mockito.mock(TaskEventService.class); + Mockito.when(SpringApplicationContext.getBean(TaskEventService.class)).thenReturn(taskEventService); - processService = PowerMockito.mock(ProcessService.class); - PowerMockito.when(SpringApplicationContext.getBean(ProcessService.class)).thenReturn(processService); + processService = Mockito.mock(ProcessService.class); + Mockito.when(SpringApplicationContext.getBean(ProcessService.class)).thenReturn(processService); taskExecuteRunningProcessor = new TaskExecuteRunningProcessor(); - channel = PowerMockito.mock(Channel.class); - taskResponseEvent = PowerMockito.mock(TaskEvent.class); + channel = Mockito.mock(Channel.class); + taskResponseEvent = Mockito.mock(TaskEvent.class); taskExecuteRunningMessage = new TaskExecuteRunningCommand("127.0.0.1:5678", " 127.0.0.1:1234", @@ -78,18 +76,5 @@ public class TaskAckProcessorTest { @Test public void testProcess() { - // Command command = taskExecuteAckCommand.convert2Command(); - // Assert.assertEquals(CommandType.TASK_EXECUTE_ACK,command.getType()); - // InetSocketAddress socketAddress = new InetSocketAddress("localhost",12345); - // PowerMockito.when(channel.remoteAddress()).thenReturn(socketAddress); - // PowerMockito.mockStatic(TaskResponseEvent.class); - // - // PowerMockito.when(TaskResponseEvent.newAck(Mockito.any(), Mockito.any(), Mockito.anyString(), - // Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(), channel)) - // .thenReturn(taskResponseEvent); - // TaskInstance taskInstance = PowerMockito.mock(TaskInstance.class); - // PowerMockito.when(processService.findTaskInstanceById(Mockito.any())).thenReturn(taskInstance); - // - // taskAckProcessor.process(channel,command); } } diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskKillResponseProcessorTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskKillResponseProcessorTest.java index 2e17302693..939db9cf86 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskKillResponseProcessorTest.java +++ b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskKillResponseProcessorTest.java @@ -27,7 +27,7 @@ import java.util.ArrayList; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.powermock.api.mockito.PowerMockito; +import org.mockito.Mockito; import io.netty.channel.Channel; @@ -45,7 +45,7 @@ public class TaskKillResponseProcessorTest { @Before public void before() { taskKillResponseProcessor = new TaskKillResponseProcessor(); - channel = PowerMockito.mock(Channel.class); + channel = Mockito.mock(Channel.class); taskKillResponseCommand = new TaskKillResponseCommand(); taskKillResponseCommand.setAppIds( new ArrayList() { diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/registry/MasterRegistryClientTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/registry/MasterRegistryClientTest.java index 23f209bbdf..762fb028ae 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/registry/MasterRegistryClientTest.java +++ b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/registry/MasterRegistryClientTest.java @@ -18,7 +18,6 @@ package org.apache.dolphinscheduler.server.master.registry; import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.doNothing; import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.NodeType; @@ -30,9 +29,7 @@ import org.apache.dolphinscheduler.server.master.task.MasterHeartBeatTask; import org.apache.dolphinscheduler.service.process.ProcessService; import org.apache.dolphinscheduler.service.registry.RegistryClient; -import java.util.Arrays; import java.util.Date; -import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -40,17 +37,13 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.springframework.test.util.ReflectionTestUtils; /** * MasterRegistryClientTest */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({RegistryClient.class}) -@PowerMockIgnore({"javax.management.*"}) +@RunWith(MockitoJUnitRunner.class) public class MasterRegistryClientTest { @InjectMocks @@ -70,12 +63,7 @@ public class MasterRegistryClientTest { @Before public void before() throws Exception { - given(registryClient.getLock(Mockito.anyString())).willReturn(true); - given(registryClient.releaseLock(Mockito.anyString())).willReturn(true); given(registryClient.getHostByEventDataPath(Mockito.anyString())).willReturn("127.0.0.1:8080"); - given(registryClient.getStoppable()).willReturn(cause -> { - - }); ReflectionTestUtils.setField(masterRegistryClient, "registryClient", registryClient); ReflectionTestUtils.setField(masterRegistryClient, "masterHeartBeatTask", masterHeartBeatTask); @@ -85,23 +73,15 @@ public class MasterRegistryClientTest { processInstance.setRestartTime(new Date()); processInstance.setHistoryCmd("xxx"); processInstance.setCommandType(CommandType.STOP); - given(processService.queryNeedFailoverProcessInstances(Mockito.anyString())) - .willReturn(Arrays.asList(processInstance)); - doNothing().when(processService).processNeedFailoverProcessInstances(Mockito.any(ProcessInstance.class)); TaskInstance taskInstance = new TaskInstance(); taskInstance.setId(1); taskInstance.setStartTime(new Date()); taskInstance.setHost("127.0.0.1:8080"); - given(processService.queryNeedFailoverTaskInstances(Mockito.anyString())) - .willReturn(Arrays.asList(taskInstance)); - given(processService.findProcessInstanceDetailById(Mockito.anyInt())).willReturn(Optional.of(processInstance)); given(registryClient.checkNodeExists(Mockito.anyString(), Mockito.any())).willReturn(true); Server server = new Server(); server.setHost("127.0.0.1"); server.setPort(8080); server.setCreateTime(new Date()); - given(registryClient.getServerList(NodeType.WORKER)).willReturn(Arrays.asList(server)); - given(registryClient.getServerList(NodeType.MASTER)).willReturn(Arrays.asList(server)); } @Test diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/MasterTaskExecThreadTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/MasterTaskExecThreadTest.java index b9cb32ff55..91027f3dc1 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/MasterTaskExecThreadTest.java +++ b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/MasterTaskExecThreadTest.java @@ -17,11 +17,11 @@ package org.apache.dolphinscheduler.server.master.runner; -import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus; -import org.apache.dolphinscheduler.plugin.task.api.enums.TaskTimeoutStrategy; import org.apache.dolphinscheduler.common.enums.TimeoutFlag; import org.apache.dolphinscheduler.dao.entity.TaskDefinition; import org.apache.dolphinscheduler.dao.entity.TaskInstance; +import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus; +import org.apache.dolphinscheduler.plugin.task.api.enums.TaskTimeoutStrategy; import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; import org.apache.dolphinscheduler.service.process.ProcessService; @@ -34,10 +34,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; -import org.powermock.api.mockito.PowerMockito; import org.springframework.context.ApplicationContext; -@RunWith(MockitoJUnitRunner.Silent.class) +@RunWith(MockitoJUnitRunner.class) @Ignore public class MasterTaskExecThreadTest { @@ -45,12 +44,9 @@ public class MasterTaskExecThreadTest { @Before public void setUp() { - ApplicationContext applicationContext = PowerMockito.mock(ApplicationContext.class); + ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); this.springApplicationContext = new SpringApplicationContext(); springApplicationContext.setApplicationContext(applicationContext); - // this.registryCenter = PowerMockito.mock(RegistryCenter.class); - // PowerMockito.when(SpringApplicationContext.getBean(RegistryCenter.class)) - // .thenReturn(this.registryCenter); ProcessService processService = Mockito.mock(ProcessService.class); Mockito.when(SpringApplicationContext.getBean(ProcessService.class)) .thenReturn(processService); @@ -58,18 +54,10 @@ public class MasterTaskExecThreadTest { taskDefinition.setTimeoutFlag(TimeoutFlag.OPEN); taskDefinition.setTimeoutNotifyStrategy(TaskTimeoutStrategy.WARN); taskDefinition.setTimeout(0); - Mockito.when(processService.findTaskDefinition(1L, 1)) - .thenReturn(taskDefinition); - // this.masterTaskExecThread = new MasterTaskExecThread(getTaskInstance()); } @Test public void testExistsValidWorkerGroup1() { - - /* - * Mockito.when(registryCenter.getWorkerGroupDirectly()).thenReturn(Sets.newHashSet()); boolean b = - * masterTaskExecThread.existsValidWorkerGroup("default"); Assert.assertFalse(b); - */ } @Test @@ -77,22 +65,12 @@ public class MasterTaskExecThreadTest { Set workerGroups = new HashSet<>(); workerGroups.add("test1"); workerGroups.add("test2"); - - /* - * Mockito.when(registryCenter.getWorkerGroupDirectly()).thenReturn(workerGroups); boolean b = - * masterTaskExecThread.existsValidWorkerGroup("default"); Assert.assertFalse(b); - */ } @Test public void testExistsValidWorkerGroup3() { Set workerGroups = new HashSet<>(); workerGroups.add("test1"); - /* - * Mockito.when(registryCenter.getWorkerGroupDirectly()).thenReturn(workerGroups); - * Mockito.when(registryCenter.getWorkerGroupNodesDirectly("test1")).thenReturn(workerGroups); boolean b = - * masterTaskExecThread.existsValidWorkerGroup("test1"); Assert.assertTrue(b); - */ } @Test @@ -100,24 +78,10 @@ public class MasterTaskExecThreadTest { ProcessService processService = Mockito.mock(ProcessService.class); Mockito.when(SpringApplicationContext.getBean(ProcessService.class)) .thenReturn(processService); - - TaskInstance taskInstance = getTaskInstance(); - Mockito.when(processService.findTaskInstanceById(252612)) - .thenReturn(taskInstance); - - Mockito.when(processService.updateTaskInstance(taskInstance)) - .thenReturn(true); - TaskDefinition taskDefinition = new TaskDefinition(); taskDefinition.setTimeoutFlag(TimeoutFlag.OPEN); taskDefinition.setTimeoutNotifyStrategy(TaskTimeoutStrategy.WARN); taskDefinition.setTimeout(0); - Mockito.when(processService.findTaskDefinition(1L, 1)) - .thenReturn(taskDefinition); - - // MasterTaskExecThread masterTaskExecThread = new MasterTaskExecThread(taskInstance); - // masterTaskExecThread.pauseTask(); - // org.junit.Assert.assertEquals(ExecutionStatus.PAUSE, taskInstance.getState()); } private TaskInstance getTaskInstance() { diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/WorkflowExecuteRunnableTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/WorkflowExecuteRunnableTest.java index 128aeb9863..2bae5c8143 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/WorkflowExecuteRunnableTest.java +++ b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/WorkflowExecuteRunnableTest.java @@ -21,14 +21,10 @@ import static org.apache.dolphinscheduler.common.Constants.CMDPARAM_COMPLEMENT_D import static org.apache.dolphinscheduler.common.Constants.CMDPARAM_COMPLEMENT_DATA_START_DATE; import static org.apache.dolphinscheduler.common.Constants.CMD_PARAM_RECOVERY_START_NODE_STRING; import static org.apache.dolphinscheduler.common.Constants.CMD_PARAM_START_NODES; -import static org.powermock.api.mockito.PowerMockito.mock; -import org.apache.dolphinscheduler.common.enums.CommandType; -import org.apache.dolphinscheduler.common.enums.Flag; import org.apache.dolphinscheduler.common.enums.ProcessExecutionTypeEnum; import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus; import org.apache.dolphinscheduler.common.graph.DAG; -import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; @@ -61,16 +57,10 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.springframework.context.ApplicationContext; -/** - * test for WorkflowExecuteThread - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({WorkflowExecuteRunnable.class}) +@RunWith(MockitoJUnitRunner.class) public class WorkflowExecuteRunnableTest { private WorkflowExecuteRunnable workflowExecuteThread; @@ -91,44 +81,32 @@ public class WorkflowExecuteRunnableTest { @Before public void init() throws Exception { - applicationContext = mock(ApplicationContext.class); + applicationContext = Mockito.mock(ApplicationContext.class); SpringApplicationContext springApplicationContext = new SpringApplicationContext(); springApplicationContext.setApplicationContext(applicationContext); config = new MasterConfig(); - Mockito.when(applicationContext.getBean(MasterConfig.class)).thenReturn(config); - - processService = mock(ProcessService.class); - Mockito.when(applicationContext.getBean(ProcessService.class)).thenReturn(processService); - - processInstanceDao = mock(ProcessInstanceDao.class); - - processInstance = mock(ProcessInstance.class); - Mockito.when(processInstance.getState()).thenReturn(WorkflowExecutionStatus.SUCCESS); - Mockito.when(processInstance.getHistoryCmd()).thenReturn(CommandType.COMPLEMENT_DATA.toString()); - Mockito.when(processInstance.getIsSubProcess()).thenReturn(Flag.NO); - Mockito.when(processInstance.getScheduleTime()).thenReturn(DateUtils.stringToDate("2020-01-01 00:00:00")); + processService = Mockito.mock(ProcessService.class); + processInstanceDao = Mockito.mock(ProcessInstanceDao.class); + processInstance = Mockito.mock(ProcessInstance.class); Map cmdParam = new HashMap<>(); cmdParam.put(CMDPARAM_COMPLEMENT_DATA_START_DATE, "2020-01-01 00:00:00"); cmdParam.put(CMDPARAM_COMPLEMENT_DATA_END_DATE, "2020-01-20 23:00:00"); - Mockito.when(processInstance.getCommandParam()).thenReturn(JSONUtils.toJsonString(cmdParam)); ProcessDefinition processDefinition = new ProcessDefinition(); processDefinition.setGlobalParamMap(Collections.emptyMap()); processDefinition.setGlobalParamList(Collections.emptyList()); Mockito.when(processInstance.getProcessDefinition()).thenReturn(processDefinition); - stateWheelExecuteThread = mock(StateWheelExecuteThread.class); - curingGlobalParamsService = mock(CuringParamsService.class); - NettyExecutorManager nettyExecutorManager = mock(NettyExecutorManager.class); - ProcessAlertManager processAlertManager = mock(ProcessAlertManager.class); - workflowExecuteThread = PowerMockito.spy( + stateWheelExecuteThread = Mockito.mock(StateWheelExecuteThread.class); + curingGlobalParamsService = Mockito.mock(CuringParamsService.class); + NettyExecutorManager nettyExecutorManager = Mockito.mock(NettyExecutorManager.class); + ProcessAlertManager processAlertManager = Mockito.mock(ProcessAlertManager.class); + workflowExecuteThread = Mockito.spy( new WorkflowExecuteRunnable(processInstance, processService, processInstanceDao, nettyExecutorManager, processAlertManager, config, stateWheelExecuteThread, curingGlobalParamsService)); - // prepareProcess init dag Field dag = WorkflowExecuteRunnable.class.getDeclaredField("dag"); dag.setAccessible(true); dag.set(workflowExecuteThread, new DAG()); - PowerMockito.doNothing().when(workflowExecuteThread, "endProcess"); } @Test @@ -136,7 +114,6 @@ public class WorkflowExecuteRunnableTest { try { Map cmdParam = new HashMap<>(); cmdParam.put(CMD_PARAM_START_NODES, "1,2,3"); - Mockito.when(processInstance.getCommandParam()).thenReturn(JSONUtils.toJsonString(cmdParam)); Class masterExecThreadClass = WorkflowExecuteRunnable.class; Method method = masterExecThreadClass.getDeclaredMethod("parseStartNodeName", String.class); method.setAccessible(true); @@ -249,19 +226,15 @@ public class WorkflowExecuteRunnableTest { processDefinition1.setCode(11L); processDefinition1.setExecutionType(ProcessExecutionTypeEnum.SERIAL_WAIT); Mockito.when(processInstance.getId()).thenReturn(225); - Mockito.when(processService.findProcessInstanceById(225)).thenReturn(processInstance); workflowExecuteThread.checkSerialProcess(processDefinition1); - Mockito.when(processInstance.getId()).thenReturn(225); Mockito.when(processInstance.getNextProcessInstanceId()).thenReturn(222); - ProcessInstance processInstance9 = new ProcessInstance(); processInstance9.setId(222); processInstance9.setProcessDefinitionCode(11L); processInstance9.setProcessDefinitionVersion(1); processInstance9.setState(WorkflowExecutionStatus.SERIAL_WAIT); - Mockito.when(processService.findProcessInstanceById(225)).thenReturn(processInstance); Mockito.when(processService.findProcessInstanceById(222)).thenReturn(processInstance9); workflowExecuteThread.checkSerialProcess(processDefinition1); } catch (Exception e) { diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/service/FailoverServiceTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/service/FailoverServiceTest.java index e53e11dcac..12f7abe9de 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/service/FailoverServiceTest.java +++ b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/service/FailoverServiceTest.java @@ -44,7 +44,6 @@ import org.apache.dolphinscheduler.service.registry.RegistryClient; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; -import java.util.Optional; import org.junit.Assert; import org.junit.Before; @@ -52,19 +51,12 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.springframework.context.ApplicationContext; import com.google.common.collect.Lists; -/** - * MasterRegistryClientTest - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({RegistryClient.class}) -@PowerMockIgnore({"javax.management.*"}) +@RunWith(MockitoJUnitRunner.class) public class FailoverServiceTest { private FailoverService failoverService; @@ -104,7 +96,6 @@ public class FailoverServiceTest { @Before public void before() throws Exception { - // init spring context ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); SpringApplicationContext springApplicationContext = new SpringApplicationContext(); springApplicationContext.setApplicationContext(applicationContext); @@ -132,10 +123,6 @@ public class FailoverServiceTest { given(registryClient.getLock(Mockito.anyString())).willReturn(true); given(registryClient.releaseLock(Mockito.anyString())).willReturn(true); - given(registryClient.getHostByEventDataPath(Mockito.anyString())).willReturn(testMasterHost); - given(registryClient.getStoppable()).willReturn(cause -> { - }); - given(registryClient.checkNodeExists(Mockito.anyString(), Mockito.any())).willReturn(true); processInstance = new ProcessInstance(); processInstance.setId(1); @@ -157,16 +144,11 @@ public class FailoverServiceTest { workerTaskInstance.setHost(testWorkerHost); workerTaskInstance.setTaskType(COMMON_TASK_TYPE); - given(processService.queryNeedFailoverTaskInstances(Mockito.anyString())) - .willReturn(Arrays.asList(masterTaskInstance, workerTaskInstance)); - given(processService.queryNeedFailoverProcessInstanceHost()).willReturn(Lists.newArrayList(testMasterHost)); given(processService.queryNeedFailoverProcessInstances(Mockito.anyString())) .willReturn(Arrays.asList(processInstance)); doNothing().when(processService).processNeedFailoverProcessInstances(Mockito.any(ProcessInstance.class)); given(processService.findValidTaskListByProcessId(Mockito.anyInt(), Mockito.anyInt())) .willReturn(Lists.newArrayList(masterTaskInstance, workerTaskInstance)); - given(processService.findProcessInstanceDetailById(Mockito.anyInt())) - .willReturn(Optional.ofNullable(processInstance)); Thread.sleep(1000); Server masterServer = new Server();