diff --git a/core/src/main/java/org/apache/servicecomb/core/transport/TransportManager.java b/core/src/main/java/org/apache/servicecomb/core/transport/TransportManager.java index 0e49da76eb6..ca0b157b141 100644 --- a/core/src/main/java/org/apache/servicecomb/core/transport/TransportManager.java +++ b/core/src/main/java/org/apache/servicecomb/core/transport/TransportManager.java @@ -40,6 +40,10 @@ public class TransportManager { private final Map transportMap = new HashMap<>(); + public Map getTransportMap() { + return transportMap; + } + public void clearTransportBeforeInit() { transports.clear(); } diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java index 2df6ab1dfb9..be59114a5e7 100644 --- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java +++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java @@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import com.google.common.annotations.VisibleForTesting; import org.apache.http.client.utils.URIBuilder; import org.apache.servicecomb.foundation.common.event.EnableExceptionPropagation; import org.apache.servicecomb.foundation.common.event.EventManager; @@ -79,6 +80,11 @@ private RegistrationManager() { initPrimary(); } + @VisibleForTesting + public static void setINSTANCE(RegistrationManager INSTANCE) { + RegistrationManager.INSTANCE = INSTANCE; + } + public MicroserviceInstance getMicroserviceInstance() { return primary.getMicroserviceInstance(); } diff --git a/inspector/pom.xml b/inspector/pom.xml index 233dfac3b86..93ab7fe85d6 100644 --- a/inspector/pom.xml +++ b/inspector/pom.xml @@ -78,8 +78,8 @@ test - org.jmockit - jmockit + org.mockito + mockito-inline test diff --git a/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorImpl.java b/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorImpl.java index 32aaa454267..6fdb6de80b7 100644 --- a/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorImpl.java +++ b/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorImpl.java @@ -42,6 +42,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response.Status; +import com.google.common.annotations.VisibleForTesting; import org.apache.commons.lang3.StringUtils; import org.apache.servicecomb.common.rest.resource.ClassPathStaticResourceHandler; import org.apache.servicecomb.common.rest.resource.StaticResourceHandler; @@ -108,6 +109,11 @@ public InspectorImpl setPropertyFactory(PriorityPropertyFactory propertyFactory) return this; } + @VisibleForTesting + public Map getSchemas() { + return schemas; + } + public InspectorImpl setSchemas(Map schemas) { this.schemas = schemas; return this; diff --git a/inspector/src/test/java/org/apache/servicecomb/inspector/internal/TestInspectorImpl.java b/inspector/src/test/java/org/apache/servicecomb/inspector/internal/TestInspectorImpl.java index e653c4aa212..634354f3906 100644 --- a/inspector/src/test/java/org/apache/servicecomb/inspector/internal/TestInspectorImpl.java +++ b/inspector/src/test/java/org/apache/servicecomb/inspector/internal/TestInspectorImpl.java @@ -63,16 +63,18 @@ import com.netflix.config.DynamicProperty; -import mockit.Deencapsulation; -import mockit.Expectations; -import mockit.Mocked; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; +import org.mockito.Mockito; public class TestInspectorImpl { static Map schemas = new LinkedHashMap<>(); static InspectorImpl inspector; + static RegistrationManager originRegistrationManagerInstance; + @BeforeClass public static void setup() throws IOException { ConfigUtil.installDynamicConfig(); @@ -82,6 +84,7 @@ public static void setup() throws IOException { .toString(TestInspectorImpl.class.getClassLoader().getResource("schema2.yaml"), StandardCharsets.UTF_8)); inspector = initInspector(null); + originRegistrationManagerInstance = RegistrationManager.INSTANCE; } private static InspectorImpl initInspector(String urlPrefix) { @@ -89,7 +92,7 @@ private static InspectorImpl initInspector(String urlPrefix) { scbEngine.getTransportManager().clearTransportBeforeInit(); if (StringUtils.isNotEmpty(urlPrefix)) { - Map transportMap = Deencapsulation.getField(scbEngine.getTransportManager(), "transportMap"); + Map transportMap = scbEngine.getTransportManager().getTransportMap(); transportMap.put(RESTFUL, new ServletRestTransport()); ClassLoaderScopeContext.setClassLoaderScopeProperty(DefinitionConst.URL_PREFIX, urlPrefix); } @@ -129,24 +132,22 @@ public void getSchemaIds() { } @Test - public void downloadSchemas_default_to_swagger(@Mocked Microservice microservice) throws IOException { + public void downloadSchemas_default_to_swagger() throws IOException { + Microservice microservice = Mockito.mock(Microservice.class); testDownloadSchemasSwagger(microservice, null); } @Test - public void downloadSchemas_swagger(@Mocked Microservice microservice) throws IOException { + public void downloadSchemas_swagger() throws IOException { + Microservice microservice = Mockito.mock(Microservice.class); testDownloadSchemasSwagger(microservice, SchemaFormat.SWAGGER); } private void testDownloadSchemasSwagger(Microservice microservice, SchemaFormat format) throws IOException { - new Expectations(RegistrationManager.INSTANCE) { - { - RegistrationManager.INSTANCE.getMicroservice(); - result = microservice; - microservice.getServiceName(); - result = "ms"; - } - }; + RegistrationManager spy = Mockito.spy(RegistrationManager.INSTANCE); + RegistrationManager.setINSTANCE(spy); + Mockito.when(spy.getMicroservice()).thenReturn(microservice); + Mockito.when(microservice.getServiceName()).thenReturn("ms"); Response response = inspector.downloadSchemas(format); Part part = response.getResult(); @@ -162,15 +163,13 @@ private void testDownloadSchemasSwagger(Microservice microservice, SchemaFormat } @Test - public void downloadSchemas_html(@Mocked Microservice microservice) throws IOException { - new Expectations(RegistrationManager.INSTANCE) { - { - RegistrationManager.INSTANCE.getMicroservice(); - result = microservice; - microservice.getServiceName(); - result = "ms"; - } - }; + public void downloadSchemas_html() throws IOException { + Microservice microservice = Mockito.mock(Microservice.class); + RegistrationManager spy = Mockito.spy(RegistrationManager.INSTANCE); + RegistrationManager.setINSTANCE(spy); + Mockito.when(spy.getMicroservice()).thenReturn(microservice); + Mockito.when(microservice.getServiceName()).thenReturn("ms"); + Response response = inspector.downloadSchemas(SchemaFormat.HTML); Part part = response.getResult(); Assertions.assertEquals("ms.html.zip", part.getSubmittedFileName()); @@ -185,14 +184,10 @@ public void downloadSchemas_html(@Mocked Microservice microservice) throws IOExc } @Test + @EnabledForJreRange(min = JRE.JAVA_9) public void downloadSchemas_failed() { - SchemaFormat format = SchemaFormat.SWAGGER; - new Expectations(format) { - { - format.getSuffix(); - result = new RuntimeExceptionWithoutStackTrace("zip failed."); - } - }; + SchemaFormat format = Mockito.spy(SchemaFormat.SWAGGER); + Mockito.doThrow(new RuntimeExceptionWithoutStackTrace("zip failed.")).when(format).getSuffix(); try (LogCollector logCollector = new LogCollector()) { Response response = inspector.downloadSchemas(format); @@ -364,9 +359,10 @@ public void priorityProperties() { @Test public void urlPrefix() { + RegistrationManager.setINSTANCE(originRegistrationManagerInstance); InspectorImpl inspector = initInspector("/webroot/rest"); - Map schemas = Deencapsulation.getField(inspector, "schemas"); + Map schemas = inspector.getSchemas(); Assertions.assertTrue(schemas.get("schema1").indexOf("/webroot/rest/metrics") > 0); } }