[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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

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

View File

@ -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());

View File

@ -50,7 +50,11 @@
<artifactId>spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<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.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<Instance> getInstanceList(int count) {
ArrayList<Instance> 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<Instance> getInstanceList(int count) {
ArrayList<Instance> 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());
}
}

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.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();

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.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;

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.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;

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.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;

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.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;

View File

@ -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 {

View File

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

View File

@ -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);
}
}

View File

@ -124,6 +124,16 @@
<artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope>
</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>
<build>
<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 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 {

View File

@ -78,6 +78,16 @@
<artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope>
</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>
<build>

View File

@ -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 {

View File

@ -75,17 +75,6 @@
<artifactId>spring-boot-starter-security</artifactId>
</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>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
@ -95,11 +84,13 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>

View File

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

View File

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

10
pom.xml
View File

@ -621,16 +621,6 @@
<!-- Default dependencies in all subprojects -->
<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>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>

View File

@ -48,6 +48,16 @@
<artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope>
</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>

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.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);
@ -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());
}

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.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 {
@ -46,7 +44,7 @@ public class PrometheusApiExceptionHandlerTest {
@MockBean
private PrometheusController prometheusController;
@Before
@BeforeEach
public void before() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@ -54,21 +52,19 @@ public class PrometheusApiExceptionHandlerTest {
@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));
}
}

View File

@ -42,26 +42,16 @@
<artifactId>spring-boot-starter</artifactId>
</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>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<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.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 {

View File

@ -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 {

View File

@ -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 {