[ISSUE #12016] upgrade to junit5 and remove junit4 (#12280)

* clean junit4

* clean junit4

* clean some test dep; fix scope

* fix test compile
This commit is contained in:
shalk(xiao kun) 2024-06-26 17:39:08 +08:00 committed by GitHub
parent 108f51083f
commit 8034da88e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
28 changed files with 188 additions and 209 deletions

View File

@ -41,8 +41,8 @@
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -63,11 +63,6 @@
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>

View File

@ -22,8 +22,6 @@ import org.junit.jupiter.api.Test;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -55,20 +53,20 @@ class InstanceBuilderTest {
@Test @Test
void testBuildFullInstance() { void testBuildFullInstance() {
InstanceBuilder builder = InstanceBuilder.newBuilder(); InstanceBuilder builder = InstanceBuilder.newBuilder();
Instance actual = builder.setServiceName(SERVICE_NAME).setClusterName(CLUSTER_NAME).setInstanceId(INSTANCE_ID) Instance actual = builder.setServiceName(SERVICE_NAME).setClusterName(CLUSTER_NAME).setInstanceId(INSTANCE_ID).setIp(IP)
.setIp(IP).setPort(PORT).setWeight(WEIGHT).setHealthy(HEALTHY).setEnabled(ENABLED) .setPort(PORT).setWeight(WEIGHT).setHealthy(HEALTHY).setEnabled(ENABLED).setEphemeral(EPHEMERAL)
.setEphemeral(EPHEMERAL).addMetadata(META_KEY, META_VALUE).build(); .addMetadata(META_KEY, META_VALUE).build();
assertThat(actual.getServiceName(), is(SERVICE_NAME)); assertEquals(actual.getServiceName(), SERVICE_NAME);
assertThat(actual.getClusterName(), is(CLUSTER_NAME)); assertEquals(actual.getClusterName(), CLUSTER_NAME);
assertThat(actual.getInstanceId(), is(INSTANCE_ID)); assertEquals(actual.getInstanceId(), INSTANCE_ID);
assertThat(actual.getIp(), is(IP)); assertEquals(actual.getIp(), IP);
assertThat(actual.getPort(), is(PORT)); assertEquals(actual.getPort(), PORT);
assertThat(actual.getWeight(), is(WEIGHT)); assertEquals(actual.getWeight(), WEIGHT);
assertThat(actual.isHealthy(), is(HEALTHY)); assertEquals(actual.isHealthy(), HEALTHY);
assertThat(actual.isEnabled(), is(ENABLED)); assertEquals(actual.isEnabled(), ENABLED);
assertThat(actual.isEphemeral(), is(EPHEMERAL)); assertEquals(actual.isEphemeral(), EPHEMERAL);
assertThat(actual.getMetadata().size(), is(1)); assertEquals(actual.getMetadata().size(), 1);
assertThat(actual.getMetadata().get(META_KEY), is(META_VALUE)); assertEquals(actual.getMetadata().get(META_KEY), META_VALUE);
} }
@Test @Test
@ -81,8 +79,8 @@ class InstanceBuilderTest {
assertNull(actual.getClusterName()); assertNull(actual.getClusterName());
assertNull(actual.getInstanceId()); assertNull(actual.getInstanceId());
assertNull(actual.getIp()); assertNull(actual.getIp());
assertThat(actual.getPort(), is(0)); assertEquals(actual.getPort(), 0);
assertThat(actual.getWeight(), is(1.0)); assertEquals(actual.getWeight(), 1.0);
assertTrue(actual.isHealthy()); assertTrue(actual.isHealthy());
assertTrue(actual.isEnabled()); assertTrue(actual.isEnabled());
assertTrue(actual.isEphemeral()); assertTrue(actual.isEphemeral());
@ -97,8 +95,8 @@ class InstanceBuilderTest {
assertNull(actual.getClusterName()); assertNull(actual.getClusterName());
assertNull(actual.getInstanceId()); assertNull(actual.getInstanceId());
assertNull(actual.getIp()); assertNull(actual.getIp());
assertThat(actual.getPort(), is(0)); assertEquals(actual.getPort(), 0);
assertThat(actual.getWeight(), is(1.0)); assertEquals(actual.getWeight(), 1.0);
assertTrue(actual.isHealthy()); assertTrue(actual.isHealthy());
assertTrue(actual.isEnabled()); assertTrue(actual.isEnabled());
assertTrue(actual.isEphemeral()); assertTrue(actual.isEphemeral());

View File

@ -50,7 +50,11 @@
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId> <groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId> <artifactId>tomcat-embed-core</artifactId>

View File

@ -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.Instance;
import com.alibaba.nacos.api.naming.pojo.builder.InstanceBuilder; import com.alibaba.nacos.api.naming.pojo.builder.InstanceBuilder;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Random; 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 { public class InstancesDiffTest {
private static List<Instance> getInstanceList(int count) {
ArrayList<Instance> list = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
list.add(new Instance());
}
return list;
}
@Test @Test
public void testGetDiff() { public void testGetDiff() {
String serviceName = "testService"; String serviceName = "testService";
@ -40,13 +51,13 @@ public class InstancesDiffTest {
instancesDiff.setRemovedInstances(Collections.singletonList(removedIns)); instancesDiff.setRemovedInstances(Collections.singletonList(removedIns));
instancesDiff.setModifiedInstances(Collections.singletonList(modifiedIns)); instancesDiff.setModifiedInstances(Collections.singletonList(modifiedIns));
Assert.assertTrue(instancesDiff.hasDifferent()); assertTrue(instancesDiff.hasDifferent());
Assert.assertTrue(instancesDiff.isAdded()); assertTrue(instancesDiff.isAdded());
Assert.assertTrue(instancesDiff.isRemoved()); assertTrue(instancesDiff.isRemoved());
Assert.assertTrue(instancesDiff.isModified()); assertTrue(instancesDiff.isModified());
Assert.assertEquals(addedIns, instancesDiff.getAddedInstances().get(0)); assertEquals(addedIns, instancesDiff.getAddedInstances().get(0));
Assert.assertEquals(removedIns, instancesDiff.getRemovedInstances().get(0)); assertEquals(removedIns, instancesDiff.getRemovedInstances().get(0));
Assert.assertEquals(modifiedIns, instancesDiff.getModifiedInstances().get(0)); assertEquals(modifiedIns, instancesDiff.getModifiedInstances().get(0));
} }
@Test @Test
@ -58,21 +69,21 @@ public class InstancesDiffTest {
InstancesDiff instancesDiff = new InstancesDiff(getInstanceList(addedCount), getInstanceList(removedCount), InstancesDiff instancesDiff = new InstancesDiff(getInstanceList(addedCount), getInstanceList(removedCount),
getInstanceList(modifiedCount)); getInstanceList(modifiedCount));
Assert.assertTrue(instancesDiff.hasDifferent()); assertTrue(instancesDiff.hasDifferent());
Assert.assertTrue(instancesDiff.isAdded()); assertTrue(instancesDiff.isAdded());
Assert.assertTrue(instancesDiff.isRemoved()); assertTrue(instancesDiff.isRemoved());
Assert.assertTrue(instancesDiff.isModified()); assertTrue(instancesDiff.isModified());
Assert.assertEquals(addedCount, instancesDiff.getAddedInstances().size()); assertEquals(addedCount, instancesDiff.getAddedInstances().size());
Assert.assertEquals(removedCount, instancesDiff.getRemovedInstances().size()); assertEquals(removedCount, instancesDiff.getRemovedInstances().size());
Assert.assertEquals(modifiedCount, instancesDiff.getModifiedInstances().size()); assertEquals(modifiedCount, instancesDiff.getModifiedInstances().size());
instancesDiff.getAddedInstances().clear(); instancesDiff.getAddedInstances().clear();
instancesDiff.getRemovedInstances().clear(); instancesDiff.getRemovedInstances().clear();
instancesDiff.getModifiedInstances().clear(); instancesDiff.getModifiedInstances().clear();
Assert.assertFalse(instancesDiff.hasDifferent()); assertFalse(instancesDiff.hasDifferent());
Assert.assertFalse(instancesDiff.hasDifferent()); assertFalse(instancesDiff.hasDifferent());
Assert.assertFalse(instancesDiff.isAdded()); assertFalse(instancesDiff.isAdded());
Assert.assertFalse(instancesDiff.isRemoved()); assertFalse(instancesDiff.isRemoved());
Assert.assertFalse(instancesDiff.isModified()); assertFalse(instancesDiff.isModified());
} }
@Test @Test
@ -86,24 +97,16 @@ public class InstancesDiffTest {
instancesDiff.setRemovedInstances(getInstanceList(removedCount)); instancesDiff.setRemovedInstances(getInstanceList(removedCount));
instancesDiff.setModifiedInstances(getInstanceList(modifiedCount)); instancesDiff.setModifiedInstances(getInstanceList(modifiedCount));
Assert.assertTrue(instancesDiff.hasDifferent()); assertTrue(instancesDiff.hasDifferent());
Assert.assertEquals(addedCount, instancesDiff.getAddedInstances().size()); assertEquals(addedCount, instancesDiff.getAddedInstances().size());
Assert.assertEquals(removedCount, instancesDiff.getRemovedInstances().size()); assertEquals(removedCount, instancesDiff.getRemovedInstances().size());
Assert.assertEquals(modifiedCount, instancesDiff.getModifiedInstances().size()); assertEquals(modifiedCount, instancesDiff.getModifiedInstances().size());
instancesDiff.getAddedInstances().clear(); instancesDiff.getAddedInstances().clear();
instancesDiff.getRemovedInstances().clear(); instancesDiff.getRemovedInstances().clear();
instancesDiff.getModifiedInstances().clear(); instancesDiff.getModifiedInstances().clear();
Assert.assertFalse(instancesDiff.hasDifferent()); assertFalse(instancesDiff.hasDifferent());
Assert.assertFalse(instancesDiff.isAdded()); assertFalse(instancesDiff.isAdded());
Assert.assertFalse(instancesDiff.isRemoved()); assertFalse(instancesDiff.isRemoved());
Assert.assertFalse(instancesDiff.isModified()); assertFalse(instancesDiff.isModified());
}
private static List<Instance> getInstanceList(int count) {
ArrayList<Instance> list = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
list.add(new Instance());
}
return list;
} }
} }

View File

@ -18,16 +18,16 @@ package com.alibaba.nacos.client.naming.listener;
import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.event.InstancesDiff; import com.alibaba.nacos.client.naming.event.InstancesDiff;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
public class NamingChangeEventTest { public class NamingChangeEventTest {
@ -35,7 +35,7 @@ public class NamingChangeEventTest {
private InstancesDiff instancesDiff; private InstancesDiff instancesDiff;
@Before @BeforeEach
public void setUp() throws Exception { public void setUp() throws Exception {
eventListener = new MockNamingEventListener(); eventListener = new MockNamingEventListener();
instancesDiff = new InstancesDiff(); instancesDiff = new InstancesDiff();

View File

@ -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.pojo.Instance;
import com.alibaba.nacos.api.naming.selector.NamingContext; import com.alibaba.nacos.api.naming.selector.NamingContext;
import com.alibaba.nacos.api.naming.selector.NamingResult; 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;

View File

@ -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.event.InstancesDiff;
import com.alibaba.nacos.client.naming.listener.AbstractNamingChangeListener; import com.alibaba.nacos.client.naming.listener.AbstractNamingChangeListener;
import com.alibaba.nacos.client.naming.listener.NamingChangeEvent; 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.Collections;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;

View File

@ -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.NamingContext;
import com.alibaba.nacos.api.naming.selector.NamingResult; import com.alibaba.nacos.api.naming.selector.NamingResult;
import com.alibaba.nacos.api.naming.selector.NamingSelector; 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.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;

View File

@ -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.InstancesChangeEvent;
import com.alibaba.nacos.client.naming.event.InstancesDiff; import com.alibaba.nacos.client.naming.event.InstancesDiff;
import com.alibaba.nacos.client.naming.listener.NamingChangeEvent; 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.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;

View File

@ -17,15 +17,15 @@
package com.alibaba.nacos.client.selector; package com.alibaba.nacos.client.selector;
import com.alibaba.nacos.client.naming.selector.NamingSelectorWrapper; 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
public class SelectorManagerTest { public class SelectorManagerTest {

View File

@ -52,11 +52,6 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -27,8 +27,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import static org.hamcrest.CoreMatchers.is; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset; import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -100,13 +99,13 @@ class TraceEventPublisherTest {
traceEventPublisher.publish(new TraceTestEvent.TraceTestEvent1()); traceEventPublisher.publish(new TraceTestEvent.TraceTestEvent1());
traceEventPublisher.publish(new TraceTestEvent.TraceTestEvent2()); traceEventPublisher.publish(new TraceTestEvent.TraceTestEvent2());
String expectedStatus = "Publisher TraceTestEvent : shutdown=false, queue= 3/8 "; String expectedStatus = "Publisher TraceTestEvent : shutdown=false, queue= 3/8 ";
assertThat(traceEventPublisher.getStatus(), is(expectedStatus)); assertEquals(traceEventPublisher.getStatus(), expectedStatus);
traceEventPublisher.addSubscriber(subscriber, TraceTestEvent.TraceTestEvent1.class); traceEventPublisher.addSubscriber(subscriber, TraceTestEvent.TraceTestEvent1.class);
ThreadUtils.sleep(2000L); ThreadUtils.sleep(2000L);
expectedStatus = "Publisher TraceTestEvent : shutdown=false, queue= 0/8 "; expectedStatus = "Publisher TraceTestEvent : shutdown=false, queue= 0/8 ";
assertThat(traceEventPublisher.getStatus(), is(expectedStatus)); assertEquals(traceEventPublisher.getStatus(), expectedStatus);
traceEventPublisher.shutdown(); traceEventPublisher.shutdown();
expectedStatus = "Publisher TraceTestEvent : shutdown= true, queue= 0/8 "; expectedStatus = "Publisher TraceTestEvent : shutdown= true, queue= 0/8 ";
assertThat(traceEventPublisher.getStatus(), is(expectedStatus)); assertEquals(traceEventPublisher.getStatus(), expectedStatus);
} }
} }

View File

@ -124,6 +124,16 @@
<artifactId>spring-boot-test-autoconfigure</artifactId> <artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

View File

@ -21,11 +21,9 @@ import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.config.server.controller.v2.HistoryControllerV2; import com.alibaba.nacos.config.server.controller.v2.HistoryControllerV2;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; 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.MockMvc;
import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 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.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@RunWith(SpringRunner.class)
@WebMvcTest(GlobalExceptionHandlerTest.class) @WebMvcTest(GlobalExceptionHandlerTest.class)
class GlobalExceptionHandlerTest { class GlobalExceptionHandlerTest {

View File

@ -78,6 +78,16 @@
<artifactId>spring-boot-test-autoconfigure</artifactId> <artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -21,11 +21,9 @@ import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.console.controller.v2.NamespaceControllerV2; import com.alibaba.nacos.console.controller.v2.NamespaceControllerV2;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; 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.MockMvc;
import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 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.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@RunWith(SpringRunner.class)
@WebMvcTest(NacosApiExceptionHandler.class) @WebMvcTest(NacosApiExceptionHandler.class)
class NacosApiExceptionHandlerTest { class NacosApiExceptionHandlerTest {

View File

@ -75,17 +75,6 @@
<artifactId>spring-boot-starter-security</artifactId> <artifactId>spring-boot-starter-security</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId> <artifactId>spring-security-test</artifactId>
@ -95,11 +84,13 @@
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId> <artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId> <groupId>org.apache.tomcat.embed</groupId>

View File

@ -62,6 +62,11 @@
<groupId>org.apache.derby</groupId> <groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId> <artifactId>derby</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -54,5 +54,11 @@
<groupId>org.springframework.ldap</groupId> <groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId> <artifactId>spring-ldap-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

10
pom.xml
View File

@ -621,16 +621,6 @@
<!-- Default dependencies in all subprojects --> <!-- Default dependencies in all subprojects -->
<dependencies> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId> <artifactId>junit-jupiter</artifactId>

View File

@ -48,6 +48,16 @@
<artifactId>spring-boot-test-autoconfigure</artifactId> <artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>

View File

@ -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.ServiceManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.prometheus.api.ApiConstants; import com.alibaba.nacos.prometheus.api.ApiConstants;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Assert; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
@ -43,7 +42,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
@ -52,9 +51,15 @@ import static org.mockito.Mockito.when;
* @author karsonto * @author karsonto
* @date 2023-02-01 10:56 * @date 2023-02-01 10:56
*/ */
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class PrometheusControllerTest { public class PrometheusControllerTest {
private final String nameSpace = "A";
private final String group = "B";
private final String name = "C";
@InjectMocks @InjectMocks
private PrometheusController prometheusController; private PrometheusController prometheusController;
@ -63,17 +68,11 @@ public class PrometheusControllerTest {
private Service service; private Service service;
private final String nameSpace = "A";
private final String group = "B";
private final String name = "C";
private List testInstanceList; private List testInstanceList;
private MockMvc mockMvc; private MockMvc mockMvc;
@Before @BeforeEach
public void setUp() throws NoSuchFieldException, IllegalAccessException, NacosException { public void setUp() throws NoSuchFieldException, IllegalAccessException, NacosException {
ServiceManager serviceManager = ServiceManager.getInstance(); ServiceManager serviceManager = ServiceManager.getInstance();
service = Service.newService(nameSpace, group, name); service = Service.newService(nameSpace, group, name);
@ -93,7 +92,7 @@ public class PrometheusControllerTest {
return instance; return instance;
} }
@After @AfterEach
public void tearDown() { public void tearDown() {
ServiceManager serviceManager = ServiceManager.getInstance(); ServiceManager serviceManager = ServiceManager.getInstance();
serviceManager.removeSingleton(service); serviceManager.removeSingleton(service);
@ -101,48 +100,41 @@ public class PrometheusControllerTest {
@Test @Test
public void testMetric() throws Exception { public void testMetric() throws Exception {
when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn( when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn(testInstanceList);
testInstanceList);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(ApiConstants.PROMETHEUS_CONTROLLER_PATH); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(ApiConstants.PROMETHEUS_CONTROLLER_PATH);
MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse(); MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus()); assertEquals(200, response.getStatus());
assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size()); assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size());
} }
@Test @Test
public void testMetricNamespace() throws Exception { public void testMetricNamespace() throws Exception {
when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn( when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn(testInstanceList);
testInstanceList); String prometheusNamespacePath = ApiConstants.PROMETHEUS_CONTROLLER_NAMESPACE_PATH.replace("{namespaceId}", nameSpace);
String prometheusNamespacePath = ApiConstants.PROMETHEUS_CONTROLLER_NAMESPACE_PATH.replace("{namespaceId}",
nameSpace);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespacePath); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespacePath);
MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse(); MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus()); assertEquals(200, response.getStatus());
assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size()); assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size());
} }
@Test @Test
public void testMetricNamespaceService() throws Exception { public void testMetricNamespaceService() throws Exception {
when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn( when(instanceServiceV2.listAllInstances(nameSpace, NamingUtils.getGroupedName(name, group))).thenReturn(testInstanceList);
testInstanceList); String prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}", nameSpace);
String prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}",
nameSpace);
prometheusNamespaceServicePath = prometheusNamespaceServicePath.replace("{service}", service.getName()); prometheusNamespaceServicePath = prometheusNamespaceServicePath.replace("{service}", service.getName());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespaceServicePath); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespaceServicePath);
MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse(); MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus()); assertEquals(200, response.getStatus());
assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size()); assertEquals(testInstanceList.size(), JacksonUtils.toObj(response.getContentAsString()).size());
} }
@Test @Test
public void testEmptyMetricNamespaceService() throws Exception { public void testEmptyMetricNamespaceService() throws Exception {
String prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}", String prometheusNamespaceServicePath = ApiConstants.PROMETHEUS_CONTROLLER_SERVICE_PATH.replace("{namespaceId}", nameSpace);
nameSpace); prometheusNamespaceServicePath = prometheusNamespaceServicePath.replace("{service}", "D"); //query non-existed service
prometheusNamespaceServicePath = prometheusNamespaceServicePath.replace("{service}",
"D"); //query non-existed service
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespaceServicePath); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(prometheusNamespaceServicePath);
MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse(); MockHttpServletResponse response = mockMvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus()); assertEquals(200, response.getStatus());
assertEquals(0, JacksonUtils.toObj(response.getContentAsString()).size()); assertEquals(0, JacksonUtils.toObj(response.getContentAsString()).size());
} }

View File

@ -19,22 +19,20 @@ package com.alibaba.nacos.prometheus.controller.exception;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException; import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.prometheus.controller.PrometheusController; import com.alibaba.nacos.prometheus.controller.PrometheusController;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; 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.MockMvc;
import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@RunWith(SpringRunner.class)
@WebMvcTest(PrometheusApiExceptionHandlerTest.class) @WebMvcTest(PrometheusApiExceptionHandlerTest.class)
public class PrometheusApiExceptionHandlerTest { public class PrometheusApiExceptionHandlerTest {
@ -46,7 +44,7 @@ public class PrometheusApiExceptionHandlerTest {
@MockBean @MockBean
private PrometheusController prometheusController; private PrometheusController prometheusController;
@Before @BeforeEach
public void before() { public void before() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
} }
@ -54,10 +52,8 @@ public class PrometheusApiExceptionHandlerTest {
@Test @Test
public void testNacosRunTimeExceptionHandler() throws Exception { public void testNacosRunTimeExceptionHandler() throws Exception {
// 设置PrometheusController的行为使其抛出NacosRuntimeException并被PrometheusApiExceptionHandler捕获处理 // 设置PrometheusController的行为使其抛出NacosRuntimeException并被PrometheusApiExceptionHandler捕获处理
when(prometheusController.metric()) when(prometheusController.metric()).thenThrow(new NacosRuntimeException(NacosException.INVALID_PARAM))
.thenThrow(new NacosRuntimeException(NacosException.INVALID_PARAM)) .thenThrow(new NacosRuntimeException(NacosException.SERVER_ERROR)).thenThrow(new NacosRuntimeException(503));
.thenThrow(new NacosRuntimeException(NacosException.SERVER_ERROR))
.thenThrow(new NacosRuntimeException(503));
// 执行请求并验证响应码 // 执行请求并验证响应码
ResultActions resultActions = mockMvc.perform(get("/prometheus")); ResultActions resultActions = mockMvc.perform(get("/prometheus"));

View File

@ -42,26 +42,16 @@
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId> <artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>

View File

@ -18,17 +18,14 @@ package com.alibaba.nacos.sys.env;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(SpringRunner.class)
@ActiveProfiles("test") @ActiveProfiles("test")
@SpringBootTest(classes = EnvUtilWithConfigTest.class) @SpringBootTest(classes = EnvUtilWithConfigTest.class)
class EnvUtilWithConfigTest { class EnvUtilWithConfigTest {

View File

@ -19,17 +19,14 @@ package com.alibaba.nacos.sys.env;
import com.alibaba.nacos.common.utils.ThreadUtils; import com.alibaba.nacos.common.utils.ThreadUtils;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(SpringRunner.class)
@ActiveProfiles("empty") @ActiveProfiles("empty")
@SpringBootTest(classes = EnvUtilWithConfigTest.class) @SpringBootTest(classes = EnvUtilWithConfigTest.class)
class EnvUtilWithoutConfigTest { class EnvUtilWithoutConfigTest {

View File

@ -17,19 +17,16 @@
package com.alibaba.nacos.sys.utils; package com.alibaba.nacos.sys.utils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(SpringRunner.class)
@ActiveProfiles("prefix") @ActiveProfiles("prefix")
@SpringBootTest(classes = PropertiesUtilTest.class) @SpringBootTest(classes = PropertiesUtilTest.class)
class PropertiesUtilTest { class PropertiesUtilTest {