diff --git a/address/pom.xml b/address/pom.xml index 2dfb65afa..36c69ca77 100644 --- a/address/pom.xml +++ b/address/pom.xml @@ -41,8 +41,8 @@ - org.springframework.boot - spring-boot-starter-test + org.springframework + spring-test test diff --git a/api/pom.xml b/api/pom.xml index 63505fc47..50da5b170 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -63,11 +63,6 @@ com.fasterxml.jackson.core jackson-databind - - junit - junit - test - org.springframework spring-test diff --git a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/builder/InstanceBuilderTest.java b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/builder/InstanceBuilderTest.java index 574db46c3..85e34bda5 100644 --- a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/builder/InstanceBuilderTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/builder/InstanceBuilderTest.java @@ -22,8 +22,6 @@ import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -55,20 +53,20 @@ class InstanceBuilderTest { @Test void testBuildFullInstance() { InstanceBuilder builder = InstanceBuilder.newBuilder(); - Instance actual = builder.setServiceName(SERVICE_NAME).setClusterName(CLUSTER_NAME).setInstanceId(INSTANCE_ID) - .setIp(IP).setPort(PORT).setWeight(WEIGHT).setHealthy(HEALTHY).setEnabled(ENABLED) - .setEphemeral(EPHEMERAL).addMetadata(META_KEY, META_VALUE).build(); - assertThat(actual.getServiceName(), is(SERVICE_NAME)); - assertThat(actual.getClusterName(), is(CLUSTER_NAME)); - assertThat(actual.getInstanceId(), is(INSTANCE_ID)); - assertThat(actual.getIp(), is(IP)); - assertThat(actual.getPort(), is(PORT)); - assertThat(actual.getWeight(), is(WEIGHT)); - assertThat(actual.isHealthy(), is(HEALTHY)); - assertThat(actual.isEnabled(), is(ENABLED)); - assertThat(actual.isEphemeral(), is(EPHEMERAL)); - assertThat(actual.getMetadata().size(), is(1)); - assertThat(actual.getMetadata().get(META_KEY), is(META_VALUE)); + Instance actual = builder.setServiceName(SERVICE_NAME).setClusterName(CLUSTER_NAME).setInstanceId(INSTANCE_ID).setIp(IP) + .setPort(PORT).setWeight(WEIGHT).setHealthy(HEALTHY).setEnabled(ENABLED).setEphemeral(EPHEMERAL) + .addMetadata(META_KEY, META_VALUE).build(); + assertEquals(actual.getServiceName(), SERVICE_NAME); + assertEquals(actual.getClusterName(), CLUSTER_NAME); + assertEquals(actual.getInstanceId(), INSTANCE_ID); + assertEquals(actual.getIp(), IP); + assertEquals(actual.getPort(), PORT); + assertEquals(actual.getWeight(), WEIGHT); + assertEquals(actual.isHealthy(), HEALTHY); + assertEquals(actual.isEnabled(), ENABLED); + assertEquals(actual.isEphemeral(), EPHEMERAL); + assertEquals(actual.getMetadata().size(), 1); + assertEquals(actual.getMetadata().get(META_KEY), META_VALUE); } @Test @@ -81,8 +79,8 @@ class InstanceBuilderTest { assertNull(actual.getClusterName()); assertNull(actual.getInstanceId()); assertNull(actual.getIp()); - assertThat(actual.getPort(), is(0)); - assertThat(actual.getWeight(), is(1.0)); + assertEquals(actual.getPort(), 0); + assertEquals(actual.getWeight(), 1.0); assertTrue(actual.isHealthy()); assertTrue(actual.isEnabled()); assertTrue(actual.isEphemeral()); @@ -97,8 +95,8 @@ class InstanceBuilderTest { assertNull(actual.getClusterName()); assertNull(actual.getInstanceId()); assertNull(actual.getIp()); - assertThat(actual.getPort(), is(0)); - assertThat(actual.getWeight(), is(1.0)); + assertEquals(actual.getPort(), 0); + assertEquals(actual.getWeight(), 1.0); assertTrue(actual.isHealthy()); assertTrue(actual.isEnabled()); assertTrue(actual.isEphemeral()); diff --git a/auth/pom.xml b/auth/pom.xml index 745aab5dd..f1af84b1a 100644 --- a/auth/pom.xml +++ b/auth/pom.xml @@ -50,7 +50,11 @@ spring-boot-starter true - + + org.springframework + spring-test + test + org.apache.tomcat.embed tomcat-embed-core diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesDiffTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesDiffTest.java index 0842558a8..af3f9ecf6 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesDiffTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesDiffTest.java @@ -18,16 +18,27 @@ package com.alibaba.nacos.client.naming.event; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.builder.InstanceBuilder; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class InstancesDiffTest { + private static List getInstanceList(int count) { + ArrayList list = new ArrayList<>(count); + for (int i = 0; i < count; i++) { + list.add(new Instance()); + } + return list; + } + @Test public void testGetDiff() { String serviceName = "testService"; @@ -40,13 +51,13 @@ public class InstancesDiffTest { instancesDiff.setRemovedInstances(Collections.singletonList(removedIns)); instancesDiff.setModifiedInstances(Collections.singletonList(modifiedIns)); - Assert.assertTrue(instancesDiff.hasDifferent()); - Assert.assertTrue(instancesDiff.isAdded()); - Assert.assertTrue(instancesDiff.isRemoved()); - Assert.assertTrue(instancesDiff.isModified()); - Assert.assertEquals(addedIns, instancesDiff.getAddedInstances().get(0)); - Assert.assertEquals(removedIns, instancesDiff.getRemovedInstances().get(0)); - Assert.assertEquals(modifiedIns, instancesDiff.getModifiedInstances().get(0)); + assertTrue(instancesDiff.hasDifferent()); + assertTrue(instancesDiff.isAdded()); + assertTrue(instancesDiff.isRemoved()); + assertTrue(instancesDiff.isModified()); + assertEquals(addedIns, instancesDiff.getAddedInstances().get(0)); + assertEquals(removedIns, instancesDiff.getRemovedInstances().get(0)); + assertEquals(modifiedIns, instancesDiff.getModifiedInstances().get(0)); } @Test @@ -58,21 +69,21 @@ public class InstancesDiffTest { InstancesDiff instancesDiff = new InstancesDiff(getInstanceList(addedCount), getInstanceList(removedCount), getInstanceList(modifiedCount)); - Assert.assertTrue(instancesDiff.hasDifferent()); - Assert.assertTrue(instancesDiff.isAdded()); - Assert.assertTrue(instancesDiff.isRemoved()); - Assert.assertTrue(instancesDiff.isModified()); - Assert.assertEquals(addedCount, instancesDiff.getAddedInstances().size()); - Assert.assertEquals(removedCount, instancesDiff.getRemovedInstances().size()); - Assert.assertEquals(modifiedCount, instancesDiff.getModifiedInstances().size()); + assertTrue(instancesDiff.hasDifferent()); + assertTrue(instancesDiff.isAdded()); + assertTrue(instancesDiff.isRemoved()); + assertTrue(instancesDiff.isModified()); + assertEquals(addedCount, instancesDiff.getAddedInstances().size()); + assertEquals(removedCount, instancesDiff.getRemovedInstances().size()); + assertEquals(modifiedCount, instancesDiff.getModifiedInstances().size()); instancesDiff.getAddedInstances().clear(); instancesDiff.getRemovedInstances().clear(); instancesDiff.getModifiedInstances().clear(); - Assert.assertFalse(instancesDiff.hasDifferent()); - Assert.assertFalse(instancesDiff.hasDifferent()); - Assert.assertFalse(instancesDiff.isAdded()); - Assert.assertFalse(instancesDiff.isRemoved()); - Assert.assertFalse(instancesDiff.isModified()); + assertFalse(instancesDiff.hasDifferent()); + assertFalse(instancesDiff.hasDifferent()); + assertFalse(instancesDiff.isAdded()); + assertFalse(instancesDiff.isRemoved()); + assertFalse(instancesDiff.isModified()); } @Test @@ -86,24 +97,16 @@ public class InstancesDiffTest { instancesDiff.setRemovedInstances(getInstanceList(removedCount)); instancesDiff.setModifiedInstances(getInstanceList(modifiedCount)); - Assert.assertTrue(instancesDiff.hasDifferent()); - Assert.assertEquals(addedCount, instancesDiff.getAddedInstances().size()); - Assert.assertEquals(removedCount, instancesDiff.getRemovedInstances().size()); - Assert.assertEquals(modifiedCount, instancesDiff.getModifiedInstances().size()); + assertTrue(instancesDiff.hasDifferent()); + assertEquals(addedCount, instancesDiff.getAddedInstances().size()); + assertEquals(removedCount, instancesDiff.getRemovedInstances().size()); + assertEquals(modifiedCount, instancesDiff.getModifiedInstances().size()); instancesDiff.getAddedInstances().clear(); instancesDiff.getRemovedInstances().clear(); instancesDiff.getModifiedInstances().clear(); - Assert.assertFalse(instancesDiff.hasDifferent()); - Assert.assertFalse(instancesDiff.isAdded()); - Assert.assertFalse(instancesDiff.isRemoved()); - Assert.assertFalse(instancesDiff.isModified()); - } - - private static List getInstanceList(int count) { - ArrayList list = new ArrayList<>(count); - for (int i = 0; i < count; i++) { - list.add(new Instance()); - } - return list; + assertFalse(instancesDiff.hasDifferent()); + assertFalse(instancesDiff.isAdded()); + assertFalse(instancesDiff.isRemoved()); + assertFalse(instancesDiff.isModified()); } } diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/listener/NamingChangeEventTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/listener/NamingChangeEventTest.java index 66278c13a..7f97bbcbb 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/listener/NamingChangeEventTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/listener/NamingChangeEventTest.java @@ -18,16 +18,16 @@ package com.alibaba.nacos.client.naming.listener; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.client.naming.event.InstancesDiff; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.Collections; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; public class NamingChangeEventTest { @@ -35,7 +35,7 @@ public class NamingChangeEventTest { private InstancesDiff instancesDiff; - @Before + @BeforeEach public void setUp() throws Exception { eventListener = new MockNamingEventListener(); instancesDiff = new InstancesDiff(); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelectorTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelectorTest.java index 7860d5112..03fad8f3c 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelectorTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelectorTest.java @@ -19,14 +19,14 @@ package com.alibaba.nacos.client.naming.selector; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.selector.NamingContext; import com.alibaba.nacos.api.naming.selector.NamingResult; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; import java.util.Random; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvokerTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvokerTest.java index 0614bca0c..8a62a1ea8 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvokerTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvokerTest.java @@ -22,12 +22,12 @@ import com.alibaba.nacos.api.naming.listener.NamingEvent; import com.alibaba.nacos.client.naming.event.InstancesDiff; import com.alibaba.nacos.client.naming.listener.AbstractNamingChangeListener; import com.alibaba.nacos.client.naming.listener.NamingChangeEvent; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Collections; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingSelectorFactoryTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingSelectorFactoryTest.java index af4290d64..3c3aa27ad 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingSelectorFactoryTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingSelectorFactoryTest.java @@ -20,16 +20,16 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.selector.NamingContext; import com.alibaba.nacos.api.naming.selector.NamingResult; import com.alibaba.nacos.api.naming.selector.NamingSelector; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingSelectorWrapperTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingSelectorWrapperTest.java index 205a662af..2d941617a 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingSelectorWrapperTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingSelectorWrapperTest.java @@ -22,17 +22,17 @@ import com.alibaba.nacos.api.naming.selector.NamingSelector; import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; import com.alibaba.nacos.client.naming.event.InstancesDiff; import com.alibaba.nacos.client.naming.listener.NamingChangeEvent; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Collections; import java.util.HashSet; import java.util.Objects; import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; diff --git a/client/src/test/java/com/alibaba/nacos/client/selector/SelectorManagerTest.java b/client/src/test/java/com/alibaba/nacos/client/selector/SelectorManagerTest.java index dd15a7ca7..0d6897711 100644 --- a/client/src/test/java/com/alibaba/nacos/client/selector/SelectorManagerTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/selector/SelectorManagerTest.java @@ -17,15 +17,15 @@ package com.alibaba.nacos.client.selector; import com.alibaba.nacos.client.naming.selector.NamingSelectorWrapper; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Set; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; public class SelectorManagerTest { diff --git a/cmdb/pom.xml b/cmdb/pom.xml index 4aa45d2b0..3a0cda7ce 100644 --- a/cmdb/pom.xml +++ b/cmdb/pom.xml @@ -52,11 +52,6 @@ org.springframework.boot spring-boot-starter-web - - org.springframework.boot - spring-boot-starter-test - test - diff --git a/common/src/test/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisherTest.java b/common/src/test/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisherTest.java index 824a3a3f5..4cf991267 100644 --- a/common/src/test/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisherTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisherTest.java @@ -27,8 +27,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.never; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; @@ -100,13 +99,13 @@ class TraceEventPublisherTest { traceEventPublisher.publish(new TraceTestEvent.TraceTestEvent1()); traceEventPublisher.publish(new TraceTestEvent.TraceTestEvent2()); String expectedStatus = "Publisher TraceTestEvent : shutdown=false, queue= 3/8 "; - assertThat(traceEventPublisher.getStatus(), is(expectedStatus)); + assertEquals(traceEventPublisher.getStatus(), expectedStatus); traceEventPublisher.addSubscriber(subscriber, TraceTestEvent.TraceTestEvent1.class); ThreadUtils.sleep(2000L); expectedStatus = "Publisher TraceTestEvent : shutdown=false, queue= 0/8 "; - assertThat(traceEventPublisher.getStatus(), is(expectedStatus)); + assertEquals(traceEventPublisher.getStatus(), expectedStatus); traceEventPublisher.shutdown(); expectedStatus = "Publisher TraceTestEvent : shutdown= true, queue= 0/8 "; - assertThat(traceEventPublisher.getStatus(), is(expectedStatus)); + assertEquals(traceEventPublisher.getStatus(), expectedStatus); } } diff --git a/config/pom.xml b/config/pom.xml index f37acf760..c10e7df37 100644 --- a/config/pom.xml +++ b/config/pom.xml @@ -124,6 +124,16 @@ spring-boot-test-autoconfigure test + + org.springframework + spring-test + test + + + org.hamcrest + hamcrest + test + diff --git a/config/src/test/java/com/alibaba/nacos/config/server/exception/GlobalExceptionHandlerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/exception/GlobalExceptionHandlerTest.java index 8f4f8ce2a..34e193a06 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/exception/GlobalExceptionHandlerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/exception/GlobalExceptionHandlerTest.java @@ -21,11 +21,9 @@ import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException; import com.alibaba.nacos.config.server.controller.v2.HistoryControllerV2; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -35,7 +33,6 @@ import org.springframework.web.context.WebApplicationContext; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -@RunWith(SpringRunner.class) @WebMvcTest(GlobalExceptionHandlerTest.class) class GlobalExceptionHandlerTest { @@ -71,4 +68,4 @@ class GlobalExceptionHandlerTest { resultActions2.andExpect(MockMvcResultMatchers.status().is(503)); } -} \ No newline at end of file +} diff --git a/console/pom.xml b/console/pom.xml index 7ae191d4a..d2dfca562 100644 --- a/console/pom.xml +++ b/console/pom.xml @@ -78,6 +78,16 @@ spring-boot-test-autoconfigure test + + org.springframework + spring-test + test + + + org.hamcrest + hamcrest + test + diff --git a/console/src/test/java/com/alibaba/nacos/console/exception/NacosApiExceptionHandlerTest.java b/console/src/test/java/com/alibaba/nacos/console/exception/NacosApiExceptionHandlerTest.java index 920a10303..be8b57a06 100644 --- a/console/src/test/java/com/alibaba/nacos/console/exception/NacosApiExceptionHandlerTest.java +++ b/console/src/test/java/com/alibaba/nacos/console/exception/NacosApiExceptionHandlerTest.java @@ -21,11 +21,9 @@ import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException; import com.alibaba.nacos.console.controller.v2.NamespaceControllerV2; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -36,7 +34,6 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -@RunWith(SpringRunner.class) @WebMvcTest(NacosApiExceptionHandler.class) class NacosApiExceptionHandlerTest { @@ -71,4 +68,4 @@ class NacosApiExceptionHandlerTest { ResultActions resultActions2 = mockMvc.perform(post("/v2/console/namespace")); resultActions2.andExpect(MockMvcResultMatchers.status().is(503)); } -} \ No newline at end of file +} diff --git a/core/pom.xml b/core/pom.xml index 934df6fea..0187e0a47 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -75,17 +75,6 @@ spring-boot-starter-security - - org.springframework.boot - spring-boot-starter-test - test - - - org.junit.vintage - junit-vintage-engine - - - org.springframework.security spring-security-test @@ -95,11 +84,13 @@ org.springframework spring-test + test org.springframework.boot spring-boot-test + test org.apache.tomcat.embed diff --git a/persistence/pom.xml b/persistence/pom.xml index a72c2f3f2..fee4f4eba 100644 --- a/persistence/pom.xml +++ b/persistence/pom.xml @@ -62,6 +62,11 @@ org.apache.derby derby + + org.springframework + spring-test + test + - \ No newline at end of file + diff --git a/plugin-default-impl/nacos-default-auth-plugin/pom.xml b/plugin-default-impl/nacos-default-auth-plugin/pom.xml index 95b618c46..11e4d389b 100644 --- a/plugin-default-impl/nacos-default-auth-plugin/pom.xml +++ b/plugin-default-impl/nacos-default-auth-plugin/pom.xml @@ -54,5 +54,11 @@ org.springframework.ldap spring-ldap-core + + + org.springframework + spring-test + test + - \ No newline at end of file + diff --git a/pom.xml b/pom.xml index 6257e8e78..aeedc9999 100644 --- a/pom.xml +++ b/pom.xml @@ -621,16 +621,6 @@ - - junit - junit - test - - - org.junit.vintage - junit-vintage-engine - test - org.junit.jupiter junit-jupiter diff --git a/prometheus/pom.xml b/prometheus/pom.xml index f36bc6785..fad764f2e 100644 --- a/prometheus/pom.xml +++ b/prometheus/pom.xml @@ -48,6 +48,16 @@ spring-boot-test-autoconfigure test + + org.springframework + spring-test + test + + + org.hamcrest + hamcrest + test + diff --git a/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/PrometheusControllerTest.java b/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/PrometheusControllerTest.java index 52f638d62..d8c9acfd6 100644 --- a/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/PrometheusControllerTest.java +++ b/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/PrometheusControllerTest.java @@ -24,14 +24,13 @@ import com.alibaba.nacos.naming.core.InstanceOperatorClientImpl; import com.alibaba.nacos.naming.core.v2.ServiceManager; import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.prometheus.api.ApiConstants; -import org.junit.After; -import org.junit.Assert; -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.InjectMocks; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; @@ -43,7 +42,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; /** @@ -52,9 +51,15 @@ import static org.mockito.Mockito.when; * @author karsonto * @date 2023-02-01 10:56 */ -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class PrometheusControllerTest { + private final String nameSpace = "A"; + + private final String group = "B"; + + private final String name = "C"; + @InjectMocks private PrometheusController prometheusController; @@ -63,17 +68,11 @@ public class PrometheusControllerTest { private Service service; - private final String nameSpace = "A"; - - private final String group = "B"; - - private final String name = "C"; - private List testInstanceList; private MockMvc mockMvc; - @Before + @BeforeEach public void setUp() throws NoSuchFieldException, IllegalAccessException, NacosException { ServiceManager serviceManager = ServiceManager.getInstance(); service = Service.newService(nameSpace, group, name); @@ -83,7 +82,7 @@ public class PrometheusControllerTest { testInstanceList.add(prepareInstance("A", "127.0.0.1", 8081, Collections.singletonMap("__meta_key", "value2"))); mockMvc = MockMvcBuilders.standaloneSetup(prometheusController).build(); } - + private Instance prepareInstance(String clusterName, String ip, int port, Map metadata) { Instance instance = new Instance(); instance.setClusterName("A"); @@ -93,7 +92,7 @@ public class PrometheusControllerTest { return instance; } - @After + @AfterEach public void tearDown() { ServiceManager serviceManager = ServiceManager.getInstance(); serviceManager.removeSingleton(service); @@ -101,48 +100,41 @@ public class PrometheusControllerTest { @Test public void testMetric() throws Exception { - when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn( - testInstanceList); + when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn(testInstanceList); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(ApiConstants.PROMETHEUS_CONTROLLER_PATH); MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse(); - Assert.assertEquals(200, response.getStatus()); + assertEquals(200, response.getStatus()); assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size()); } @Test public void testMetricNamespace() throws Exception { - when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn( - testInstanceList); - String prometheusNamespacePath = ApiConstants.PROMETHEUS_CONTROLLER_NAMESPACE_PATH.replace("{namespaceId}", - nameSpace); + when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn(testInstanceList); + String prometheusNamespacePath = ApiConstants.PROMETHEUS_CONTROLLER_NAMESPACE_PATH.replace("{namespaceId}", nameSpace); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespacePath); MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse(); - Assert.assertEquals(200, response.getStatus()); + assertEquals(200, response.getStatus()); assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size()); } @Test public void testMetricNamespaceService() throws Exception { - when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn( - testInstanceList); - String prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}", - nameSpace); + when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn(testInstanceList); + String prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}", nameSpace); prometheusNamespaceServicePath = prometheusNamespaceServicePath.replace("{service}", service.getName()); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespaceServicePath); MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse(); - Assert.assertEquals(200, response.getStatus()); + assertEquals(200, response.getStatus()); assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size()); } @Test public void testEmptyMetricNamespaceService() throws Exception { - String prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}", - nameSpace); - prometheusNamespaceServicePath = prometheusNamespaceServicePath.replace("{service}", - "D"); //query non-existed service + String prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}", nameSpace); + prometheusNamespaceServicePath = prometheusNamespaceServicePath.replace("{service}", "D"); //query non-existed service MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespaceServicePath); MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse(); - Assert.assertEquals(200, response.getStatus()); + assertEquals(200, response.getStatus()); assertEquals(0, JacksonUtils.toObj(response.getContentAsString()).size()); } diff --git a/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/exception/PrometheusApiExceptionHandlerTest.java b/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/exception/PrometheusApiExceptionHandlerTest.java index 0b1f8b9ba..373010264 100644 --- a/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/exception/PrometheusApiExceptionHandlerTest.java +++ b/prometheus/src/test/java/com/alibaba/nacos/prometheus/controller/exception/PrometheusApiExceptionHandlerTest.java @@ -19,56 +19,52 @@ package com.alibaba.nacos.prometheus.controller.exception; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException; import com.alibaba.nacos.prometheus.controller.PrometheusController; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; + import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -@RunWith(SpringRunner.class) @WebMvcTest(PrometheusApiExceptionHandlerTest.class) public class PrometheusApiExceptionHandlerTest { - + private MockMvc mockMvc; - + @Autowired private WebApplicationContext context; - + @MockBean private PrometheusController prometheusController; - - @Before + + @BeforeEach public void before() { mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); } - + @Test public void testNacosRunTimeExceptionHandler() throws Exception { // 设置PrometheusController的行为,使其抛出NacosRuntimeException并被PrometheusApiExceptionHandler捕获处理 - when(prometheusController.metric()) - .thenThrow(new NacosRuntimeException(NacosException.INVALID_PARAM)) - .thenThrow(new NacosRuntimeException(NacosException.SERVER_ERROR)) - .thenThrow(new NacosRuntimeException(503)); - + when(prometheusController.metric()).thenThrow(new NacosRuntimeException(NacosException.INVALID_PARAM)) + .thenThrow(new NacosRuntimeException(NacosException.SERVER_ERROR)).thenThrow(new NacosRuntimeException(503)); + // 执行请求并验证响应码 - ResultActions resultActions = mockMvc.perform(get("/prometheus")); + ResultActions resultActions = mockMvc.perform(get("/prometheus")); resultActions.andExpect(MockMvcResultMatchers.status().is(NacosException.INVALID_PARAM)); - + // 执行请求并验证响应码 - ResultActions resultActions1 = mockMvc.perform(get("/prometheus")); + ResultActions resultActions1 = mockMvc.perform(get("/prometheus")); resultActions1.andExpect(MockMvcResultMatchers.status().is(NacosException.SERVER_ERROR)); - + // 执行请求并验证响应码 - ResultActions resultActions2 = mockMvc.perform(get("/prometheus")); + ResultActions resultActions2 = mockMvc.perform(get("/prometheus")); resultActions2.andExpect(MockMvcResultMatchers.status().is(503)); } -} \ No newline at end of file +} diff --git a/sys/pom.xml b/sys/pom.xml index 62f573160..ccb4c7a48 100644 --- a/sys/pom.xml +++ b/sys/pom.xml @@ -42,26 +42,16 @@ spring-boot-starter - - org.springframework.boot - spring-boot-starter-test - test - - - org.junit.vintage - junit-vintage-engine - - - - org.springframework spring-test + test org.springframework.boot spring-boot-test + test com.alibaba.nacos diff --git a/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithConfigTest.java b/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithConfigTest.java index c247773a2..97e6db6a4 100644 --- a/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithConfigTest.java +++ b/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithConfigTest.java @@ -18,17 +18,14 @@ package com.alibaba.nacos.sys.env; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; import static org.junit.jupiter.api.Assertions.assertEquals; -@RunWith(SpringRunner.class) @ActiveProfiles("test") @SpringBootTest(classes = EnvUtilWithConfigTest.class) class EnvUtilWithConfigTest { diff --git a/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithoutConfigTest.java b/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithoutConfigTest.java index 65bbbc18c..42f36c4ad 100644 --- a/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithoutConfigTest.java +++ b/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilWithoutConfigTest.java @@ -19,17 +19,14 @@ package com.alibaba.nacos.sys.env; import com.alibaba.nacos.common.utils.ThreadUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; import static org.junit.jupiter.api.Assertions.assertEquals; -@RunWith(SpringRunner.class) @ActiveProfiles("empty") @SpringBootTest(classes = EnvUtilWithConfigTest.class) class EnvUtilWithoutConfigTest { diff --git a/sys/src/test/java/com/alibaba/nacos/sys/utils/PropertiesUtilTest.java b/sys/src/test/java/com/alibaba/nacos/sys/utils/PropertiesUtilTest.java index 78be84b5a..659dee75c 100644 --- a/sys/src/test/java/com/alibaba/nacos/sys/utils/PropertiesUtilTest.java +++ b/sys/src/test/java/com/alibaba/nacos/sys/utils/PropertiesUtilTest.java @@ -17,19 +17,16 @@ package com.alibaba.nacos.sys.utils; import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; import java.util.Map; import java.util.Properties; import static org.junit.jupiter.api.Assertions.assertEquals; -@RunWith(SpringRunner.class) @ActiveProfiles("prefix") @SpringBootTest(classes = PropertiesUtilTest.class) class PropertiesUtilTest {