From 06d7b51e9c038bcb5d9bb73159cda208cd25dd86 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Tue, 15 Nov 2022 09:33:36 +0800 Subject: [PATCH] fix:fix discovery junit. (#728) --- CHANGELOG.md | 1 + .../discovery/PolarisDiscoveryClientTest.java | 2 - .../PolarisServiceDiscoveryTest.java | 94 +++++++------------ 3 files changed, 37 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe2e804bb..fb97594b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,3 +11,4 @@ - [docs:update README.](https://github.com/Tencent/spring-cloud-tencent/pull/719) - [Code optimization & correct comment](https://github.com/Tencent/spring-cloud-tencent/pull/723) - [fix:fix javadoc.](https://github.com/Tencent/spring-cloud-tencent/pull/726) +- [fix:fix discovery junit.](https://github.com/Tencent/spring-cloud-tencent/pull/728) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientTest.java index 303f39736..e69a433e8 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientTest.java @@ -62,13 +62,11 @@ public class PolarisDiscoveryClientTest { @Test public void testGetServices() { - when(polarisServiceDiscovery.getServices()).thenReturn(singletonList(SERVICE_PROVIDER)); List services = client.getServices(); assertThat(services).contains(SERVICE_PROVIDER).size().isEqualTo(1); - } @Test diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java index d9c3eea29..e568cfc2a 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java @@ -19,89 +19,67 @@ package com.tencent.cloud.polaris.discovery; import java.util.List; -import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.polaris.api.exception.PolarisException; -import com.tencent.polaris.api.pojo.ServiceKey; -import com.tencent.polaris.test.mock.discovery.NamingServer; -import com.tencent.polaris.test.mock.discovery.NamingService; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import com.tencent.polaris.api.pojo.DefaultInstance; +import com.tencent.polaris.api.pojo.DefaultServiceInstances; +import com.tencent.polaris.api.pojo.ServiceInfo; +import com.tencent.polaris.api.rpc.InstancesResponse; +import com.tencent.polaris.api.rpc.ServicesResponse; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.cloud.client.ServiceInstance; import org.springframework.context.annotation.Configuration; -import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST; -import static com.tencent.polaris.test.common.Consts.PORT; import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Test for {@link PolarisServiceDiscovery}. * * @author Haotian Zhang */ -public class PolarisServiceDiscoveryTest { +@RunWith(MockitoJUnitRunner.class) - private static NamingServer namingServer; - - private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() - .withConfiguration(AutoConfigurations.of( - PolarisContextAutoConfiguration.class, - PolarisServiceDiscoveryTest.PolarisPropertiesConfiguration.class, - PolarisDiscoveryClientConfiguration.class, - PolarisDiscoveryAutoConfiguration.class, - PolarisContextAutoConfiguration.class)) - .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) - .withPropertyValues("server.port=" + PORT) - .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") - .withPropertyValues("spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST) - .withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx"); - - @BeforeClass - public static void beforeClass() throws Exception { - namingServer = NamingServer.startNamingServer(10081); - - // add service with 3 instances - NamingService.InstanceParameter instanceParameter = new NamingService.InstanceParameter(); - instanceParameter.setHealthy(true); - instanceParameter.setIsolated(false); - instanceParameter.setWeight(100); - ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER); - namingServer.getNamingService().batchAddInstances(serviceKey, PORT, 3, instanceParameter); - } +public class PolarisServiceDiscoveryTest { - @AfterClass - public static void afterClass() { - if (null != namingServer) { - namingServer.terminate(); - } - } + @Mock + private PolarisDiscoveryHandler polarisDiscoveryHandler; + @InjectMocks + private PolarisServiceDiscovery polarisServiceDiscovery; @Test public void testGetInstances() { - this.contextRunner.run(context -> { - PolarisServiceDiscovery polarisServiceDiscovery = context.getBean(PolarisServiceDiscovery.class); - List serviceInstances = polarisServiceDiscovery.getInstances(SERVICE_PROVIDER); - assertThat(serviceInstances.isEmpty()).isFalse(); - assertThat(serviceInstances).hasSize(3); - assertThat(serviceInstances.get(0).getPort()).isEqualTo(PORT); - assertThat(serviceInstances.get(1).getPort()).isEqualTo(PORT + 1); - assertThat(serviceInstances.get(2).getPort()).isEqualTo(PORT + 2); - }); + DefaultServiceInstances mockDefaultServiceInstances = mock(DefaultServiceInstances.class); + when(mockDefaultServiceInstances.getInstances()).thenReturn(singletonList(mock(DefaultInstance.class))); + InstancesResponse mockInstancesResponse = mock(InstancesResponse.class); + when(mockInstancesResponse.toServiceInstances()).thenReturn(mockDefaultServiceInstances); + when(polarisDiscoveryHandler.getHealthyInstances(anyString())).thenReturn(mockInstancesResponse); + + List serviceInstances = polarisServiceDiscovery.getInstances(SERVICE_PROVIDER); + + assertThat(serviceInstances).isNotEmpty(); } @Test public void testGetServices() throws PolarisException { - this.contextRunner.run(context -> { - PolarisServiceDiscovery polarisServiceDiscovery = context.getBean(PolarisServiceDiscovery.class); - List services = polarisServiceDiscovery.getServices(); - assertThat(services.size()).isEqualTo(1); - }); + ServiceInfo mockServiceInfo = mock(ServiceInfo.class); + when(mockServiceInfo.getService()).thenReturn(SERVICE_PROVIDER); + ServicesResponse mockServicesResponse = mock(ServicesResponse.class); + when(mockServicesResponse.getServices()).thenReturn(singletonList(mockServiceInfo)); + when(polarisDiscoveryHandler.getServices()).thenReturn(mockServicesResponse); + + List services = polarisServiceDiscovery.getServices(); + assertThat(services).size().isEqualTo(1); } @Configuration