|
|
|
@ -16,22 +16,16 @@
|
|
|
|
|
|
|
|
|
|
package com.alibaba.cloud.nacos.registry;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.InvocationHandler;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration;
|
|
|
|
|
import com.alibaba.nacos.api.NacosFactory;
|
|
|
|
|
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.AfterAll;
|
|
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.mockito.MockedStatic;
|
|
|
|
|
import org.mockito.Mockito;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
@ -40,17 +34,13 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author L.cm
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(PowerMockRunner.class)
|
|
|
|
|
@PowerMockIgnore("javax.management.*")
|
|
|
|
|
@PowerMockRunnerDelegate(SpringRunner.class)
|
|
|
|
|
@PrepareForTest({ NacosFactory.class })
|
|
|
|
|
@SpringBootTest(classes = NacosRegistrationCustomizerTest.TestConfig.class,
|
|
|
|
|
properties = { "spring.application.name=myTestService1",
|
|
|
|
|
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848" },
|
|
|
|
@ -60,28 +50,25 @@ public class NacosRegistrationCustomizerTest {
|
|
|
|
|
@Autowired
|
|
|
|
|
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
|
|
|
|
|
|
|
|
|
|
private static MockedStatic<NacosFactory> nacosFactoryMockedStatic;
|
|
|
|
|
static {
|
|
|
|
|
try {
|
|
|
|
|
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
|
|
|
|
|
Properties.class);
|
|
|
|
|
MethodProxy.proxy(method, new InvocationHandler() {
|
|
|
|
|
@Override
|
|
|
|
|
public Object invoke(Object proxy, Method method, Object[] args)
|
|
|
|
|
throws Throwable {
|
|
|
|
|
return new MockNamingService();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
nacosFactoryMockedStatic = Mockito.mockStatic(NacosFactory.class);
|
|
|
|
|
nacosFactoryMockedStatic.when(() -> NacosFactory.createNamingService((Properties) any()))
|
|
|
|
|
.thenReturn(new MockNamingService());
|
|
|
|
|
}
|
|
|
|
|
@AfterAll
|
|
|
|
|
public static void finished() {
|
|
|
|
|
if (nacosFactoryMockedStatic != null) {
|
|
|
|
|
nacosFactoryMockedStatic.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextLoads() throws Exception {
|
|
|
|
|
NacosRegistration registration = nacosAutoServiceRegistration.getRegistration();
|
|
|
|
|
Map<String, String> metadata = registration.getMetadata();
|
|
|
|
|
Assert.assertEquals("test1", metadata.get("test1"));
|
|
|
|
|
Assertions.assertEquals("test1", metadata.get("test1"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|