From 4e2bcb9277e72828848f54376d68cf614d23824d Mon Sep 17 00:00:00 2001 From: "shalk(xiao kun)" Date: Wed, 15 May 2024 10:37:53 +0800 Subject: [PATCH] module address upgrade junit4 to junit5 (#12084) --- .../AddressServerGeneratorManagerTest.java | 54 +++++++++-------- .../component/AddressServerManagerTests.java | 22 +++---- .../AddressServerClusterControllerTest.java | 58 +++++++++---------- .../controller/ServerListControllerTest.java | 38 ++++++------ 4 files changed, 88 insertions(+), 84 deletions(-) diff --git a/address/src/test/java/com/alibaba/nacos/address/component/AddressServerGeneratorManagerTest.java b/address/src/test/java/com/alibaba/nacos/address/component/AddressServerGeneratorManagerTest.java index 497c94dea..95e15e3c9 100644 --- a/address/src/test/java/com/alibaba/nacos/address/component/AddressServerGeneratorManagerTest.java +++ b/address/src/test/java/com/alibaba/nacos/address/component/AddressServerGeneratorManagerTest.java @@ -18,55 +18,59 @@ package com.alibaba.nacos.address.component; import com.alibaba.nacos.address.constant.AddressServerConstants; import com.alibaba.nacos.api.naming.pojo.Instance; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; -public class AddressServerGeneratorManagerTest { - +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class AddressServerGeneratorManagerTest { + @Test - public void testGenerateProductName() { + void testGenerateProductName() { AddressServerGeneratorManager manager = new AddressServerGeneratorManager(); final String blankName = manager.generateProductName(""); - Assert.assertEquals(AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME, blankName); + assertEquals(AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME, blankName); final String defaultName = manager.generateProductName(AddressServerConstants.DEFAULT_PRODUCT); - Assert.assertEquals(AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME, defaultName); + assertEquals(AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME, defaultName); final String testName = manager.generateProductName("test"); - Assert.assertEquals("nacos.as.test", testName); + assertEquals("nacos.as.test", testName); } - + @Test - public void testGenerateInstancesByIps() { + void testGenerateInstancesByIps() { AddressServerGeneratorManager manager = new AddressServerGeneratorManager(); final List empty = manager.generateInstancesByIps(null, null, null, null); - Assert.assertNotNull(empty); - Assert.assertTrue(empty.isEmpty()); + assertNotNull(empty); + assertTrue(empty.isEmpty()); String[] ipArray = new String[]{"192.168.3.1:8848", "192.168.3.2:8848", "192.168.3.3:8848"}; final List instanceList = manager.generateInstancesByIps("DEFAULT_GROUP@@nacos.as.test", "test", "test", ipArray); - Assert.assertNotNull(instanceList); - Assert.assertFalse(instanceList.isEmpty()); - Assert.assertEquals(3, instanceList.size()); + assertNotNull(instanceList); + assertFalse(instanceList.isEmpty()); + assertEquals(3, instanceList.size()); final Instance instance1 = instanceList.get(0); - Assert.assertEquals("192.168.3.1", instance1.getIp()); + assertEquals("192.168.3.1", instance1.getIp()); final Instance instance2 = instanceList.get(1); - Assert.assertEquals("192.168.3.2", instance2.getIp()); + assertEquals("192.168.3.2", instance2.getIp()); final Instance instance3 = instanceList.get(2); - Assert.assertEquals("192.168.3.3", instance3.getIp()); + assertEquals("192.168.3.3", instance3.getIp()); } - + @Test - public void testGenerateResponseIps() { + void testGenerateResponseIps() { final List instanceList = new ArrayList<>(); Instance instance1 = new Instance(); instance1.setIp("192.168.3.1"); @@ -92,19 +96,19 @@ public class AddressServerGeneratorManagerTest { .append("192.168.3.1:8848").append('\n') .append("192.168.3.2:8848").append('\n') .append("192.168.3.3:8848").append('\n'); - Assert.assertEquals(ret.toString(), ipListStr); + assertEquals(ret.toString(), ipListStr); } - + @Test - public void testGenerateNacosServiceName() { + void testGenerateNacosServiceName() { AddressServerGeneratorManager manager = new AddressServerGeneratorManager(); final String containDefault = manager.generateNacosServiceName("DEFAULT_GROUP@@test"); - Assert.assertEquals("DEFAULT_GROUP@@test", containDefault); + assertEquals("DEFAULT_GROUP@@test", containDefault); final String product = manager.generateNacosServiceName("product"); - Assert.assertEquals("DEFAULT_GROUP@@product", product); + assertEquals("DEFAULT_GROUP@@product", product); } } diff --git a/address/src/test/java/com/alibaba/nacos/address/component/AddressServerManagerTests.java b/address/src/test/java/com/alibaba/nacos/address/component/AddressServerManagerTests.java index 25c6441db..af22fd03d 100644 --- a/address/src/test/java/com/alibaba/nacos/address/component/AddressServerManagerTests.java +++ b/address/src/test/java/com/alibaba/nacos/address/component/AddressServerManagerTests.java @@ -17,38 +17,38 @@ package com.alibaba.nacos.address.component; import com.alibaba.nacos.address.constant.AddressServerConstants; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class AddressServerManagerTests { +class AddressServerManagerTests { private static final AddressServerManager ADDRESS_SERVER_MANAGER = new AddressServerManager(); - + @Test - public void getRawProductName() { + void getRawProductName() { assertEquals(AddressServerConstants.DEFAULT_PRODUCT, ADDRESS_SERVER_MANAGER.getRawProductName("")); assertEquals(AddressServerConstants.DEFAULT_PRODUCT, ADDRESS_SERVER_MANAGER.getRawProductName(AddressServerConstants.DEFAULT_PRODUCT)); assertEquals("otherProduct", ADDRESS_SERVER_MANAGER.getRawProductName("otherProduct")); } - + @Test - public void getDefaultClusterNameIfEmpty() { + void getDefaultClusterNameIfEmpty() { assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER, ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty("")); assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER, ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty(AddressServerConstants.DEFAULT_GET_CLUSTER)); assertEquals("otherServerList", ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty("otherServerList")); } - + @Test - public void testGetRawClusterName() { + void testGetRawClusterName() { assertEquals("serverList", ADDRESS_SERVER_MANAGER.getRawClusterName("serverList")); assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER, ADDRESS_SERVER_MANAGER.getRawClusterName("")); } - + @Test - public void testSplitIps() { + void testSplitIps() { final String[] emptyArr = ADDRESS_SERVER_MANAGER.splitIps(""); assertEquals(0, emptyArr.length); final String[] one = ADDRESS_SERVER_MANAGER.splitIps("192.168.1.12:8848"); diff --git a/address/src/test/java/com/alibaba/nacos/address/controller/AddressServerClusterControllerTest.java b/address/src/test/java/com/alibaba/nacos/address/controller/AddressServerClusterControllerTest.java index aa7f8eddb..132f56c98 100644 --- a/address/src/test/java/com/alibaba/nacos/address/controller/AddressServerClusterControllerTest.java +++ b/address/src/test/java/com/alibaba/nacos/address/controller/AddressServerClusterControllerTest.java @@ -26,13 +26,13 @@ import com.alibaba.nacos.naming.core.InstanceOperator; import com.alibaba.nacos.naming.core.v2.ServiceManager; import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager; import com.alibaba.nacos.naming.core.v2.pojo.Service; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -40,8 +40,8 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@RunWith(MockitoJUnitRunner.class) -public class AddressServerClusterControllerTest { +@ExtendWith(MockitoExtension.class) +class AddressServerClusterControllerTest { @Mock private InstanceOperator instanceOperator; @@ -53,9 +53,9 @@ public class AddressServerClusterControllerTest { private ClusterOperator clusterOperator; private MockMvc mockMvc; - - @Before - public void before() { + + @BeforeEach + void before() { mockMvc = MockMvcBuilders.standaloneSetup( new AddressServerClusterController(instanceOperator, metadataManager, clusterOperator, new AddressServerManager(), new AddressServerGeneratorManager())).build(); @@ -63,30 +63,30 @@ public class AddressServerClusterControllerTest { .newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false); ServiceManager.getInstance().getSingleton(service); } - - @After - public void tearDown() { + + @AfterEach + void tearDown() { Service service = Service .newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false); ServiceManager.getInstance().removeSingleton(service); } - + @Test - public void testPostCluster() throws Exception { + void testPostCluster() throws Exception { mockMvc.perform(post("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") .param("ips", "192.168.3.1,192.168.3.2")).andExpect(status().isOk()); } - + @Test - public void testPostClusterWithErrorIps() throws Exception { + void testPostClusterWithErrorIps() throws Exception { mockMvc.perform(post("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") .param("ips", "192.168.1")).andExpect(status().isBadRequest()); } - + @Test - public void testPostClusterThrowException() throws Exception { + void testPostClusterThrowException() throws Exception { Mockito.doThrow(new NacosException(500, "create service error")).when(clusterOperator) .updateClusterMetadata(Mockito.eq(Constants.DEFAULT_NAMESPACE_ID), Mockito.eq( @@ -97,34 +97,34 @@ public class AddressServerClusterControllerTest { .param("ips", "192.168.1")).andExpect(status().isInternalServerError()); } - + @Test - public void testDeleteCluster() throws Exception { + void testDeleteCluster() throws Exception { mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") .param("ips", "192.168.3.1,192.168.3.2")).andExpect(status().isOk()); } - + @Test - public void testDeleteClusterCannotFindService() throws Exception { + void testDeleteClusterCannotFindService() throws Exception { tearDown(); mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") .param("ips", "192.168.3.1,192.168.3.2")).andExpect(status().isNotFound()); } - + @Test - public void testDeleteClusterEmptyIps() throws Exception { + void testDeleteClusterEmptyIps() throws Exception { mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") .param("ips", "")).andExpect(status().isBadRequest()); } - + @Test - public void testDeleteClusterErrorIps() throws Exception { + void testDeleteClusterErrorIps() throws Exception { mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") .param("ips", "192.168.1")).andExpect(status().isBadRequest()); } - + @Test - public void testDeleteClusterThrowException() throws Exception { + void testDeleteClusterThrowException() throws Exception { Mockito.doThrow(new NacosException(500, "remove service error")).when(instanceOperator) .removeInstance(Mockito.eq(Constants.DEFAULT_NAMESPACE_ID), Mockito.eq( Constants.DEFAULT_GROUP + AddressServerConstants.GROUP_SERVICE_NAME_SEP + "nacos.as.default"), diff --git a/address/src/test/java/com/alibaba/nacos/address/controller/ServerListControllerTest.java b/address/src/test/java/com/alibaba/nacos/address/controller/ServerListControllerTest.java index 44c27b3db..702504832 100644 --- a/address/src/test/java/com/alibaba/nacos/address/controller/ServerListControllerTest.java +++ b/address/src/test/java/com/alibaba/nacos/address/controller/ServerListControllerTest.java @@ -26,12 +26,12 @@ import com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata; import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager; import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata; import com.alibaba.nacos.naming.core.v2.pojo.Service; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -43,8 +43,8 @@ import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@RunWith(MockitoJUnitRunner.class) -public class ServerListControllerTest { +@ExtendWith(MockitoExtension.class) +class ServerListControllerTest { @Mock private NamingMetadataManager metadataManager; @@ -55,23 +55,23 @@ public class ServerListControllerTest { private Service service; private MockMvc mockMvc; - - @Before - public void before() { + + @BeforeEach + void before() { this.mockMvc = MockMvcBuilders.standaloneSetup( new ServerListController(new AddressServerGeneratorManager(), metadataManager, serviceStorage)).build(); service = Service .newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false); ServiceManager.getInstance().getSingleton(service); } - - @After - public void tearDown() { + + @AfterEach + void tearDown() { ServiceManager.getInstance().removeSingleton(service); } - + @Test - public void testGetCluster() throws Exception { + void testGetCluster() throws Exception { final Service service = Service .newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false); @@ -86,16 +86,16 @@ public class ServerListControllerTest { when(serviceStorage.getData(service)).thenReturn(serviceInfo); mockMvc.perform(get("/nacos/serverList")).andExpect(status().isOk()); } - + @Test - public void testGetClusterCannotFindService() throws Exception { + void testGetClusterCannotFindService() throws Exception { tearDown(); mockMvc.perform(get("/default/serverList")).andExpect(status().isNotFound()); } - + @Test - public void testGetClusterCannotFindCluster() throws Exception { + void testGetClusterCannotFindCluster() throws Exception { mockMvc.perform(get("/nacos/serverList")).andExpect(status().isNotFound()); }