Skip to content

Commit

Permalink
[Improvement][UT] Upgrade jUnit to 5.+ (apache#10976) (apache#11332)
Browse files Browse the repository at this point in the history
* [Improvement][UT] Upgrade jUnit to 5.+ (apache#10976)

* Refactor AlertServerTest with jUnit-5 as an example
  • Loading branch information
EricGao888 authored and xdu-chenrj committed Oct 13, 2022
1 parent 79020c1 commit 0b154eb
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<artifactId>dolphinscheduler-alert-server</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>

<dependencies>
<!-- dolphinscheduler -->
<dependency>
Expand Down Expand Up @@ -158,4 +157,5 @@
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}
9 changes: 6 additions & 3 deletions dolphinscheduler-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<artifactId>dolphinscheduler-api</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>

<dependencyManagement>
<dependencies>
<dependency>
Expand All @@ -38,7 +37,6 @@
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- dolphinscheduler -->
<dependency>
Expand Down Expand Up @@ -158,6 +156,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
Expand Down Expand Up @@ -192,7 +196,6 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<testResources>
<testResource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ public void testQueryTaskListPaging() {
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());
}

Expand All @@ -91,9 +93,9 @@ public void testForceTaskSuccess() throws Exception {
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());
Expand Down
41 changes: 29 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.boot.version>2.6.1</spring.boot.version>
<java.version>1.8</java.version>
<junit.version>4.13.1</junit.version>
<junit.version>5.9.0</junit.version>
<mockito.version>3.9.0</mockito.version>
<spotbugs.version>3.1.12</spotbugs.version>
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
<maven-javadoc-plugin.version>2.10.3</maven-javadoc-plugin.version>
<maven-source-plugin.version>2.4</maven-source-plugin.version>
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
<maven-surefire-plugin.version>3.0.0-M6</maven-surefire-plugin.version>
<maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
<rpm-maven-plugion.version>2.2.0</rpm-maven-plugion.version>
Expand Down Expand Up @@ -309,6 +310,13 @@
<artifactId>dolphinscheduler-tools</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>

</dependencyManagement>
Expand All @@ -321,9 +329,25 @@
of the submodules.
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -581,13 +605,6 @@
<jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>${maven-surefire-plugin.version}</version>
</dependency>
</dependencies>
</plugin>

<!-- jenkins plugin jacoco report-->
Expand Down

0 comments on commit 0b154eb

Please sign in to comment.