Replace powermock with mockito in Nacos config

pull/2441/head
sorie
parent c4c167b593
commit 299726a8b8

@ -63,22 +63,6 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </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> </dependencies>
</project> </project>

@ -16,20 +16,14 @@
package com.alibaba.cloud.nacos; 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.client.NacosPropertySourceLocator;
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration; import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.NacosConfigService; import com.alibaba.nacos.client.config.NacosConfigService;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito; import org.mockito.invocation.InvocationOnMock;
import org.powermock.api.support.MethodProxy; import org.mockito.stubbing.Answer;
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.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 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.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; 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 com.alibaba.cloud.nacos.NacosConfigurationExtConfigTests.TestConfig;
import static org.assertj.core.api.Assertions.assertThat; 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; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
/** /**
* TODO refactor, remove powermock.
* *
* @author xiaojing * @author xiaojing
* @author freeman * @author freeman
*/ */
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ NacosConfigService.class })
@SpringBootTest(classes = TestConfig.class, webEnvironment = NONE, properties = { @SpringBootTest(classes = TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=myTestService1", "spring.profiles.active=dev,test", "spring.application.name=myTestService1", "spring.profiles.active=dev,test",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848", "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.shared-dataids=common1.properties,common2.properties",
"spring.cloud.nacos.config.accessKey=test-accessKey", "spring.cloud.nacos.config.accessKey=test-accessKey",
"spring.cloud.nacos.config.secretKey=test-secretKey", "spring.cloud.nacos.config.secretKey=test-secretKey",
"spring.cloud.bootstrap.enabled=true" "spring.cloud.bootstrap.enabled=true" })
})
public class NacosConfigurationExtConfigTests { 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 @Autowired
private Environment environment; private Environment environment;
@ -132,16 +69,69 @@ public class NacosConfigurationExtConfigTests {
@Autowired @Autowired
private NacosConfigProperties properties; 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 @Test
public void contextLoads() throws Exception { public void contextLoads() throws Exception {
assertThat(locator).isNotNull(); assertThat(locator).isNotNull();
assertThat(properties).isNotNull(); assertThat(properties).isNotNull();
assertThat("config1").isEqualTo(environment.getProperty("test-ext-config1")); assertThat(environment.getProperty("test-ext-config1")).isEqualTo("config1");
assertThat("config2").isEqualTo(environment.getProperty("test-ext-config2")); assertThat(environment.getProperty("test-ext-config2")).isEqualTo("config2");
assertThat("common1").isEqualTo(environment.getProperty("test-common1")); assertThat(environment.getProperty("test-common1")).isEqualTo("common1");
assertThat("common2").isEqualTo(environment.getProperty("test-common2")); assertThat(environment.getProperty("test-common2")).isEqualTo("common2");
} }
@Configuration @Configuration

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

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

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

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

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

@ -16,22 +16,13 @@
package com.alibaba.cloud.nacos.endpoint; package com.alibaba.cloud.nacos.endpoint;
import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import com.alibaba.cloud.nacos.NacosConfigAutoConfiguration; import com.alibaba.cloud.nacos.NacosConfigAutoConfiguration;
import com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration; import com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration;
import com.alibaba.cloud.nacos.NacosConfigProperties; import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory; import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
import com.alibaba.nacos.client.config.NacosConfigService; import org.junit.jupiter.api.Test;
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.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health.Builder; 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.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
/** /**
* TODO refactor, remove powermock.
* *
* @author xiaojing * @author xiaojing
* @author freeman * @author freeman
*/ */
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ NacosConfigService.class })
@SpringBootTest(classes = NacosConfigEndpointTests.TestConfig.class, webEnvironment = NONE, properties = { @SpringBootTest(classes = NacosConfigEndpointTests.TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=test-name", "spring.application.name=test-name",
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848", "spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
"spring.cloud.nacos.config.file-extension=properties", "spring.cloud.nacos.config.file-extension=properties",
"spring.cloud.bootstrap.enabled=true" "spring.cloud.bootstrap.enabled=true" })
})
public class NacosConfigEndpointTests { 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 @Autowired
private NacosConfigProperties properties; private NacosConfigProperties properties;

Loading…
Cancel
Save