Replace powermock with mockito in Nacos config

pull/2441/head
sorie 3 years ago
parent c4c167b593
commit 299726a8b8

@ -63,22 +63,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock.modules.test.powermockito/powermock-modules-test-powermockito -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -16,20 +16,14 @@
package com.alibaba.cloud.nacos;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.NacosConfigService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.support.MethodProxy;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -37,23 +31,20 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static com.alibaba.cloud.nacos.NacosConfigurationExtConfigTests.TestConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
/**
* TODO refactor, remove powermock.
*
* @author xiaojing
* @author freeman
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ NacosConfigService.class })
@SpringBootTest(classes = TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=myTestService1", "spring.profiles.active=dev,test",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
@ -66,63 +57,9 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties",
"spring.cloud.nacos.config.accessKey=test-accessKey",
"spring.cloud.nacos.config.secretKey=test-secretKey",
"spring.cloud.bootstrap.enabled=true"
})
"spring.cloud.bootstrap.enabled=true" })
public class NacosConfigurationExtConfigTests {
static {
try {
// when(any(ConfigService.class).getConfig(eq("test-name.properties"),
// eq("test-group"), any())).thenReturn("user.name=hello");
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
String.class, String.class, long.class);
MethodProxy.proxy(method, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if ("test-name.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "user.name=hello\nuser.age=12";
}
if ("test-name-dev.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "user.name=dev";
}
if ("ext-config-common01.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "test-ext-config1=config1\ntest-ext-config2=config1";
}
if ("ext-config-common02.properties".equals(args[0])
&& "GLOBAL_GROUP".equals(args[1])) {
return "test-ext-config2=config2";
}
if ("common1.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "test-common1=common1\ntest-common2=common1";
}
if ("common2.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "test-common2=common2";
}
return "";
}
});
}
catch (Exception ignore) {
ignore.printStackTrace();
}
}
@Autowired
private Environment environment;
@ -132,16 +69,69 @@ public class NacosConfigurationExtConfigTests {
@Autowired
private NacosConfigProperties properties;
static {
try {
NacosConfigService mockedNacosConfigService = Mockito
.mock(NacosConfigService.class);
when(mockedNacosConfigService.getConfig(any(), any(), anyLong()))
.thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocationOnMock)
throws Throwable {
String dataId = invocationOnMock.getArgument(0, String.class);
String group = invocationOnMock.getArgument(1, String.class);
if ("test-name.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "user.name=hello\nuser.age=12";
}
if ("test-name-dev.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "user.name=dev";
}
if ("ext-config-common01.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "test-ext-config1=config1\ntest-ext-config2=config1";
}
if ("ext-config-common02.properties".equals(dataId)
&& "GLOBAL_GROUP".equals(group)) {
return "test-ext-config2=config2";
}
if ("common1.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "test-common1=common1\ntest-common2=common1";
}
if ("common2.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "test-common2=common2";
}
return "";
}
});
ReflectionTestUtils.setField(NacosConfigManager.class, "service",
mockedNacosConfigService);
}
catch (NacosException ignored) {
ignored.printStackTrace();
}
}
@Test
public void contextLoads() throws Exception {
assertThat(locator).isNotNull();
assertThat(properties).isNotNull();
assertThat("config1").isEqualTo(environment.getProperty("test-ext-config1"));
assertThat("config2").isEqualTo(environment.getProperty("test-ext-config2"));
assertThat("common1").isEqualTo(environment.getProperty("test-common1"));
assertThat("common2").isEqualTo(environment.getProperty("test-common2"));
assertThat(environment.getProperty("test-ext-config1")).isEqualTo("config1");
assertThat(environment.getProperty("test-ext-config2")).isEqualTo("config2");
assertThat(environment.getProperty("test-common1")).isEqualTo("common1");
assertThat(environment.getProperty("test-common2")).isEqualTo("common2");
}
@Configuration

@ -16,8 +16,6 @@
package com.alibaba.cloud.nacos;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import com.alibaba.cloud.nacos.NacosConfigProperties.Config;
@ -26,14 +24,10 @@ import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpoint;
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
import com.alibaba.nacos.client.config.NacosConfigService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.support.MethodProxy;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -41,21 +35,19 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
/**
* TODO refactor, remove powermock.
*
* @author xiaojing
* @author freeman
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ NacosConfigService.class })
@SpringBootTest(classes = NacosConfigurationNewTest.TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=myTestService1", "spring.profiles.active=dev,test",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
@ -74,58 +66,61 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.config.shared-configs[1]=common2.properties",
"spring.cloud.nacos.config.accessKey=test-accessKey",
"spring.cloud.nacos.config.secretKey=test-secretKey",
"spring.cloud.bootstrap.enabled=true"
})
"spring.cloud.bootstrap.enabled=true" })
public class NacosConfigurationNewTest {
static {
try {
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
String.class, String.class, long.class);
MethodProxy.proxy(method, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if ("test-name.properties".equals(args[0])
&& "test-group".equals(args[1])) {
return "user.name=hello\nuser.age=12";
}
if ("test-name-dev.properties".equals(args[0])
&& "test-group".equals(args[1])) {
return "user.name=dev";
}
if ("ext-config-common01.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "test-ext-config1=config1\ntest-ext-config2=config1";
}
if ("ext-config-common02.properties".equals(args[0])
&& "GLOBAL_GROUP".equals(args[1])) {
return "test-ext-config2=config2";
}
if ("common1.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "test-common1=common1\ntest-common2=common1";
}
if ("common2.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "test-common2=common2";
}
return "";
}
});
NacosConfigService mockedNacosConfigService = Mockito
.mock(NacosConfigService.class);
when(mockedNacosConfigService.getConfig(any(), any(), anyLong()))
.thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocationOnMock)
throws Throwable {
String dataId = invocationOnMock.getArgument(0, String.class);
String group = invocationOnMock.getArgument(1, String.class);
if ("test-name.properties".equals(dataId)
&& "test-group".equals(group)) {
return "user.name=hello\nuser.age=12";
}
if ("test-name-dev.properties".equals(dataId)
&& "test-group".equals(group)) {
return "user.name=dev";
}
if ("ext-config-common01.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "test-ext-config1=config1\ntest-ext-config2=config1";
}
if ("ext-config-common02.properties".equals(dataId)
&& "GLOBAL_GROUP".equals(group)) {
return "test-ext-config2=config2";
}
if ("common1.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "test-common1=common1\ntest-common2=common1";
}
if ("common2.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "test-common2=common2";
}
return "";
}
});
ReflectionTestUtils.setField(NacosConfigManager.class, "service",
mockedNacosConfigService);
}
catch (Exception ignore) {
ignore.printStackTrace();
}
}

@ -16,8 +16,6 @@
package com.alibaba.cloud.nacos;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
@ -25,14 +23,10 @@ import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpoint;
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
import com.alibaba.nacos.client.config.NacosConfigService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.support.MethodProxy;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -40,23 +34,19 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
/**
* TODO refactor, remove powermock.
*
* @author zkz
* @author freeman
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({ "javax.management.*", "javax.xml.parsers.*",
"com.sun.org.apache.xerces.internal.jaxp.*", "org.w3c.dom.*" })
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ NacosConfigService.class })
@SpringBootTest(classes = NacosConfigurationNoSuffixTest.TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=app-no-suffix", "spring.profiles.active=dev",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
@ -73,87 +63,93 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.config.shared-dataids=shared-data1.properties,shared-data2.xml",
"spring.cloud.nacos.config.accessKey=test-accessKey",
"spring.cloud.nacos.config.secretKey=test-secretKey",
"spring.cloud.bootstrap.enabled=true"
})
"spring.cloud.bootstrap.enabled=true" })
public class NacosConfigurationNoSuffixTest {
static {
try {
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
String.class, String.class, long.class);
MethodProxy.proxy(method, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if ("app-no-suffix".equals(args[0]) && "test-group".equals(args[1])) {
return "test-no-suffix=value-no-suffix-1";
}
if ("app-no-suffix.properties".equals(args[0])
&& "test-group".equals(args[1])) {
return "test-no-suffix=value-no-suffix-2";
}
if ("test-no-suffix-name".equals(args[0])
&& "test-group".equals(args[1])) {
return "test-no-suffix-assign=assign-value-no-suffix-111";
}
if ("test-no-suffix-name.properties".equals(args[0])
&& "test-group".equals(args[1])) {
return "test-no-suffix-assign=assign-value-no-suffix-222";
}
if ("test-no-suffix-name-dev.properties".equals(args[0])
&& "test-group".equals(args[1])) {
return "test-no-suffix-assign=assign-dev-value-no-suffix-333";
}
if ("ext-json-test.json".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "{\n" + " \"people\":{\n"
+ " \"firstName\":\"Brett\",\n"
+ " \"lastName\":\"McLaughlin\"\n" + " }\n"
+ "}";
}
if ("ext-config-common02.properties".equals(args[0])
&& "GLOBAL_GROUP".equals(args[1])) {
return "global-ext-config=global-config-value-2";
}
if ("shared-data1.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "shared-name=shared-value-1";
}
if ("shared-data2.xml".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
+ " <Service name=\"Catalina\"> \n"
+ " <Connector value=\"第二个连接器\"> \n"
+ " <open>开启服务</open> \n"
+ " <init>初始化一下</init> \n"
+ " <process>\n" + " <top>\n"
+ " <first>one</first>\n"
+ " <sencond value=\"two\">\n"
+ " <third>three</third>\n"
+ " </sencond>\n"
+ " </top>\n" + " </process> \n"
+ " <destory>销毁一下</destory> \n"
+ " <close>关闭服务</close> \n"
+ " </Connector> \n" + " </Service> \n"
+ "</Server> ";
}
return "";
}
});
NacosConfigService mockedNacosConfigService = Mockito
.mock(NacosConfigService.class);
when(mockedNacosConfigService.getConfig(any(), any(), anyLong()))
.thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocationOnMock)
throws Throwable {
String dataId = invocationOnMock.getArgument(0, String.class);
String group = invocationOnMock.getArgument(1, String.class);
if ("app-no-suffix".equals(dataId)
&& "test-group".equals(group)) {
return "test-no-suffix=value-no-suffix-1";
}
if ("app-no-suffix.properties".equals(dataId)
&& "test-group".equals(group)) {
return "test-no-suffix=value-no-suffix-2";
}
if ("test-no-suffix-name".equals(dataId)
&& "test-group".equals(group)) {
return "test-no-suffix-assign=assign-value-no-suffix-111";
}
if ("test-no-suffix-name.properties".equals(dataId)
&& "test-group".equals(group)) {
return "test-no-suffix-assign=assign-value-no-suffix-222";
}
if ("test-no-suffix-name-dev.properties".equals(dataId)
&& "test-group".equals(group)) {
return "test-no-suffix-assign=assign-dev-value-no-suffix-333";
}
if ("ext-json-test.json".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "{\n" + " \"people\":{\n"
+ " \"firstName\":\"Brett\",\n"
+ " \"lastName\":\"McLaughlin\"\n"
+ " }\n" + "}";
}
if ("ext-config-common02.properties".equals(dataId)
&& "GLOBAL_GROUP".equals(group)) {
return "global-ext-config=global-config-value-2";
}
if ("shared-data1.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "shared-name=shared-value-1";
}
if ("shared-data2.xml".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
+ " <Service name=\"Catalina\"> \n"
+ " <Connector value=\"第二个连接器\"> \n"
+ " <open>开启服务</open> \n"
+ " <init>初始化一下</init> \n"
+ " <process>\n"
+ " <top>\n"
+ " <first>one</first>\n"
+ " <sencond value=\"two\">\n"
+ " <third>three</third>\n"
+ " </sencond>\n"
+ " </top>\n"
+ " </process> \n"
+ " <destory>销毁一下</destory> \n"
+ " <close>关闭服务</close> \n"
+ " </Connector> \n" + " </Service> \n"
+ "</Server> ";
}
return "";
}
});
ReflectionTestUtils.setField(NacosConfigManager.class, "service",
mockedNacosConfigService);
}
catch (Exception ignore) {
ignore.printStackTrace();
}
}

@ -16,8 +16,6 @@
package com.alibaba.cloud.nacos;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
@ -25,14 +23,10 @@ import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpoint;
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
import com.alibaba.nacos.client.config.NacosConfigService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.support.MethodProxy;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -40,21 +34,19 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
/**
* TODO refactor, remove powermock.
*
* @author xiaojing
* @author freeman
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ NacosConfigService.class })
@SpringBootTest(classes = NacosConfigurationTests.TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=myTestService1", "spring.profiles.active=dev,test",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
@ -72,53 +64,57 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties",
"spring.cloud.nacos.config.accessKey=test-accessKey",
"spring.cloud.nacos.config.secretKey=test-secretKey",
"spring.cloud.bootstrap.enabled=true"
})
"spring.cloud.bootstrap.enabled=true" })
public class NacosConfigurationTests {
static {
try {
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
String.class, String.class, long.class);
MethodProxy.proxy(method, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if ("test-name.properties".equals(args[0])
&& "test-group".equals(args[1])) {
return "user.name=hello\nuser.age=12";
}
if ("test-name-dev.properties".equals(args[0])
&& "test-group".equals(args[1])) {
return "user.name=dev";
}
if ("ext-config-common01.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "test-ext-config1=config1\ntest-ext-config2=config1";
}
if ("ext-config-common02.properties".equals(args[0])
&& "GLOBAL_GROUP".equals(args[1])) {
return "test-ext-config2=config2";
}
if ("common1.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "test-common1=common1\ntest-common2=common1";
}
if ("common2.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "test-common2=common2";
}
return "";
}
});
NacosConfigService mockedNacosConfigService = Mockito
.mock(NacosConfigService.class);
when(mockedNacosConfigService.getConfig(any(), any(), anyLong()))
.thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocationOnMock)
throws Throwable {
String dataId = invocationOnMock.getArgument(0, String.class);
String group = invocationOnMock.getArgument(1, String.class);
if ("test-name.properties".equals(dataId)
&& "test-group".equals(group)) {
return "user.name=hello\nuser.age=12";
}
if ("test-name-dev.properties".equals(dataId)
&& "test-group".equals(group)) {
return "user.name=dev";
}
if ("ext-config-common01.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "test-ext-config1=config1\ntest-ext-config2=config1";
}
if ("ext-config-common02.properties".equals(dataId)
&& "GLOBAL_GROUP".equals(group)) {
return "test-ext-config2=config2";
}
if ("common1.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "test-common1=common1\ntest-common2=common1";
}
if ("common2.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "test-common2=common2";
}
return "";
}
});
ReflectionTestUtils.setField(NacosConfigManager.class, "service",
mockedNacosConfigService);
}
catch (Exception ignore) {

@ -16,8 +16,6 @@
package com.alibaba.cloud.nacos;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
@ -25,14 +23,10 @@ import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpoint;
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
import com.alibaba.nacos.client.config.NacosConfigService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.support.MethodProxy;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -40,22 +34,19 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
/**
* TODO refactor, remove powermock.
*
* @author zkz
* @author freeman
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({ "javax.management.*", "javax.xml.parsers.*",
"com.sun.org.apache.xerces.internal.jaxp.*", "org.w3c.dom.*" })
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ NacosConfigService.class })
@SpringBootTest(classes = NacosConfigurationXmlJsonTest.TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=xmlApp", "spring.profiles.active=dev",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
@ -73,97 +64,106 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.config.shared-dataids=shared-data1.properties,shared-data.json",
"spring.cloud.nacos.config.accessKey=test-accessKey",
"spring.cloud.nacos.config.secretKey=test-secretKey",
"spring.cloud.bootstrap.enabled=true"
})
"spring.cloud.bootstrap.enabled=true" })
public class NacosConfigurationXmlJsonTest {
static {
try {
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
String.class, String.class, long.class);
MethodProxy.proxy(method, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if ("xmlApp.xml".equals(args[0]) && "test-group".equals(args[1])) {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<top>\n"
+ " <first>one</first>\n"
+ " <sencond value=\"two\">\n"
+ " <third>three</third>\n" + " </sencond>\n"
+ "</top>";
}
if ("test-name.xml".equals(args[0]) && "test-group".equals(args[1])) {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
+ " <Service name=\"Catalina\"> \n"
+ " <Connector value=\"第二个连接器\"> \n"
+ " <open>开启服务</open> \n"
+ " <init>初始化一下</init> \n"
+ " <process>\n" + " <top>\n"
+ " <first>one</first>\n"
+ " <sencond value=\"two\">\n"
+ " <third>three</third>\n"
+ " </sencond>\n"
+ " </top>\n" + " </process> \n"
+ " <destory>销毁一下</destory> \n"
+ " <close>关闭服务</close> \n"
+ " </Connector> \n" + " </Service> \n"
+ "</Server> ";
}
if ("test-name-dev.xml".equals(args[0])
&& "test-group".equals(args[1])) {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<application android:label=\"@string/app_name\" android:icon=\"@drawable/osg\">\n"
+ " <activity android:name=\".osgViewer\"\n"
+ " android:label=\"@string/app_name\" android:screenOrientation=\"landscape\">\n"
+ " <intent-filter>\n"
+ " <action android:name=\"android.intent.action.MAIN\" />\n"
+ " <category android:name=\"android.intent.category.LAUNCHER\" />\n"
+ " </intent-filter>\n" + " </activity>\n"
+ "</application>";
}
if ("ext-json-test.json".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "{\n" + " \"people\":{\n"
+ " \"firstName\":\"Brett\",\n"
+ " \"lastName\":\"McLaughlin\"\n" + " }\n"
+ "}";
}
if ("ext-config-common02.properties".equals(args[0])
&& "GLOBAL_GROUP".equals(args[1])) {
return "global-ext-config=global-config-value-2";
}
if ("shared-data1.properties".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "shared-name=shared-value-1";
}
if ("shared-data.json".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "{\n" + " \"test\" : {\n"
+ " \"name\" : \"test\",\n"
+ " \"list\" : [\n" + " {\n"
+ " \"name\" :\"listname1\",\n"
+ " \"age\":1\n" + " },\n"
+ " {\n"
+ " \"name\" :\"listname2\",\n"
+ " \"age\":2\n" + " }\n"
+ " ],\n" + " \"metadata\" : {\n"
+ " \"intKey\" : 123,\n"
+ " \"booleanKey\" : true\n" + " }\n"
+ " }\n" + "}";
}
return "";
}
});
NacosConfigService mockedNacosConfigService = Mockito
.mock(NacosConfigService.class);
when(mockedNacosConfigService.getConfig(any(), any(), anyLong()))
.thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocationOnMock)
throws Throwable {
String dataId = invocationOnMock.getArgument(0, String.class);
String group = invocationOnMock.getArgument(1, String.class);
if ("xmlApp.xml".equals(dataId)
&& "test-group".equals(group)) {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<top>\n" + " <first>one</first>\n"
+ " <sencond value=\"two\">\n"
+ " <third>three</third>\n"
+ " </sencond>\n" + "</top>";
}
if ("test-name.xml".equals(dataId)
&& "test-group".equals(group)) {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
+ " <Service name=\"Catalina\"> \n"
+ " <Connector value=\"第二个连接器\"> \n"
+ " <open>开启服务</open> \n"
+ " <init>初始化一下</init> \n"
+ " <process>\n"
+ " <top>\n"
+ " <first>one</first>\n"
+ " <sencond value=\"two\">\n"
+ " <third>three</third>\n"
+ " </sencond>\n"
+ " </top>\n"
+ " </process> \n"
+ " <destory>销毁一下</destory> \n"
+ " <close>关闭服务</close> \n"
+ " </Connector> \n" + " </Service> \n"
+ "</Server> ";
}
if ("test-name-dev.xml".equals(dataId)
&& "test-group".equals(group)) {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<application android:label=\"@string/app_name\" android:icon=\"@drawable/osg\">\n"
+ " <activity android:name=\".osgViewer\"\n"
+ " android:label=\"@string/app_name\" android:screenOrientation=\"landscape\">\n"
+ " <intent-filter>\n"
+ " <action android:name=\"android.intent.action.MAIN\" />\n"
+ " <category android:name=\"android.intent.category.LAUNCHER\" />\n"
+ " </intent-filter>\n"
+ " </activity>\n" + "</application>";
}
if ("ext-json-test.json".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "{\n" + " \"people\":{\n"
+ " \"firstName\":\"Brett\",\n"
+ " \"lastName\":\"McLaughlin\"\n"
+ " }\n" + "}";
}
if ("ext-config-common02.properties".equals(dataId)
&& "GLOBAL_GROUP".equals(group)) {
return "global-ext-config=global-config-value-2";
}
if ("shared-data1.properties".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "shared-name=shared-value-1";
}
if ("shared-data.json".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "{\n" + " \"test\" : {\n"
+ " \"name\" : \"test\",\n"
+ " \"list\" : [\n" + " {\n"
+ " \"name\" :\"listname1\",\n"
+ " \"age\":1\n"
+ " },\n" + " {\n"
+ " \"name\" :\"listname2\",\n"
+ " \"age\":2\n"
+ " }\n" + " ],\n"
+ " \"metadata\" : {\n"
+ " \"intKey\" : 123,\n"
+ " \"booleanKey\" : true\n"
+ " }\n" + " }\n" + "}";
}
return "";
}
});
ReflectionTestUtils.setField(NacosConfigManager.class, "service",
mockedNacosConfigService);
}
catch (Exception ignore) {

@ -16,20 +16,13 @@
package com.alibaba.cloud.nacos;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import com.alibaba.nacos.client.config.NacosConfigService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.support.MethodProxy;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -37,45 +30,48 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
/**
* TODO refactor, remove powermock.
*
* @author xiaojing
* @author freeman
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ NacosConfigService.class })
@SpringBootTest(classes = NacosFileExtensionTest.TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=test-name",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
"spring.cloud.nacos.config.file-extension=yaml",
"spring.cloud.bootstrap.enabled=true"
})
"spring.cloud.bootstrap.enabled=true" })
public class NacosFileExtensionTest {
static {
try {
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
String.class, String.class, long.class);
MethodProxy.proxy(method, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if ("test-name.yaml".equals(args[0])
&& "DEFAULT_GROUP".equals(args[1])) {
return "user:\n name: hello\n age: 12\n---\nuser:\n gender: male";
}
return "";
}
});
NacosConfigService mockedNacosConfigService = Mockito
.mock(NacosConfigService.class);
when(mockedNacosConfigService.getConfig(any(), any(), anyLong()))
.thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocationOnMock)
throws Throwable {
String dataId = invocationOnMock.getArgument(0, String.class);
String group = invocationOnMock.getArgument(1, String.class);
if ("test-name.yaml".equals(dataId)
&& "DEFAULT_GROUP".equals(group)) {
return "user:\n name: hello\n age: 12\n---\nuser:\n gender: male";
}
return "";
}
});
ReflectionTestUtils.setField(NacosConfigManager.class, "service",
mockedNacosConfigService);
}
catch (Exception ignore) {
@ -90,9 +86,9 @@ public class NacosFileExtensionTest {
@Test
public void contextLoads() throws Exception {
Assert.assertEquals(environment.getProperty("user.name"), "hello");
Assert.assertEquals(environment.getProperty("user.age"), "12");
Assert.assertEquals(environment.getProperty("user.gender"), "male");
Assertions.assertEquals(environment.getProperty("user.name"), "hello");
Assertions.assertEquals(environment.getProperty("user.age"), "12");
Assertions.assertEquals(environment.getProperty("user.gender"), "male");
}
@Configuration

@ -16,22 +16,13 @@
package com.alibaba.cloud.nacos.endpoint;
import java.lang.reflect.Method;
import java.util.Map;
import com.alibaba.cloud.nacos.NacosConfigAutoConfiguration;
import com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
import com.alibaba.nacos.client.config.NacosConfigService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.support.MethodProxy;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health.Builder;
@ -39,44 +30,22 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
/**
* TODO refactor, remove powermock.
*
* @author xiaojing
* @author freeman
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ NacosConfigService.class })
@SpringBootTest(classes = NacosConfigEndpointTests.TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=test-name",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
"spring.cloud.nacos.config.file-extension=properties",
"spring.cloud.bootstrap.enabled=true"
})
"spring.cloud.bootstrap.enabled=true" })
public class NacosConfigEndpointTests {
static {
try {
Method method = PowerMockito.method(NacosConfigService.class,
"getServerStatus");
MethodProxy.proxy(method, (proxy, method1, args) -> "UP");
}
catch (Exception ignore) {
ignore.printStackTrace();
}
}
@Autowired
private NacosConfigProperties properties;

Loading…
Cancel
Save