From 870dbdc415ea0a9d2fc404defe27af08128ebfcb Mon Sep 17 00:00:00 2001 From: Tian Luo Date: Sun, 23 Oct 2022 18:47:32 +0800 Subject: [PATCH] migrate generator-core to mockito (#3424) --- .../swagger-generator/generator-core/pom.xml | 6 +-- .../servicecomb/swagger/TestSwaggerUtils.java | 52 ++++++++----------- .../generator/core/TestSwaggerUtils.java | 10 +--- 3 files changed, 26 insertions(+), 42 deletions(-) diff --git a/swagger/swagger-generator/generator-core/pom.xml b/swagger/swagger-generator/generator-core/pom.xml index ea8c3b93fb2..872090c0b15 100644 --- a/swagger/swagger-generator/generator-core/pom.xml +++ b/swagger/swagger-generator/generator-core/pom.xml @@ -50,9 +50,9 @@ - org.jmockit - jmockit - provided + org.mockito + mockito-inline + test org.junit.jupiter diff --git a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java index a4a639662e5..3080c09c27b 100644 --- a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java +++ b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java @@ -32,8 +32,8 @@ import io.swagger.models.Response; import io.swagger.models.Swagger; import io.swagger.util.Yaml; -import mockit.Expectations; -import mockit.Mocked; +import org.mockito.MockedStatic; +import org.mockito.Mockito; public class TestSwaggerUtils { @@ -47,45 +47,35 @@ public void swaggerToStringNormal() { } @Test - public void swaggerToStringException(@Mocked Swagger swagger) { - new Expectations() { - { - swagger.getBasePath(); - result = new RuntimeExceptionWithoutStackTrace(); - } - }; + public void swaggerToStringException() { + Swagger swagger = Mockito.mock(Swagger.class); + Mockito.when(swagger.getBasePath()).thenThrow(new RuntimeExceptionWithoutStackTrace()); ServiceCombException exception = Assertions.assertThrows(ServiceCombException.class, () -> SwaggerUtils.swaggerToString(swagger)); Assertions.assertEquals("Convert swagger to string failed, ", exception.getMessage()); } @Test - public void parseSwaggerUrlNormal(@Mocked URL url) throws IOException { + public void parseSwaggerUrlNormal() throws IOException { String content = "swagger: \"2.0\""; - new Expectations(IOUtils.class) { - { - IOUtils.toString(url, StandardCharsets.UTF_8); - result = content; - } - }; - - Swagger swagger = Yaml.mapper().readValue(content, Swagger.class); - Swagger result = SwaggerUtils.parseSwagger(url); - Assertions.assertEquals(swagger, result); + URL url = Mockito.mock(URL.class); + try (MockedStatic ioUtilsMockedStatic = Mockito.mockStatic(IOUtils.class)) { + ioUtilsMockedStatic.when(() -> IOUtils.toString(url, StandardCharsets.UTF_8)).thenReturn(content); + Swagger swagger = Yaml.mapper().readValue(content, Swagger.class); + Swagger result = SwaggerUtils.parseSwagger(url); + Assertions.assertEquals(swagger, result); + } } @Test - public void parseSwaggerUrlException(@Mocked URL url) throws IOException { - new Expectations(IOUtils.class) { - { - IOUtils.toString(url, StandardCharsets.UTF_8); - result = new RuntimeExceptionWithoutStackTrace("failed"); - } - }; - - ServiceCombException exception = Assertions.assertThrows(ServiceCombException.class, - () -> SwaggerUtils.parseSwagger(url)); - Assertions.assertTrue(exception.getMessage().contains("Parse swagger from url failed, ")); + public void parseSwaggerUrlException() throws IOException { + URL url = Mockito.mock(URL.class); + try (MockedStatic ioUtilsMockedStatic = Mockito.mockStatic(IOUtils.class)) { + ioUtilsMockedStatic.when(() -> IOUtils.toString(url, StandardCharsets.UTF_8)).thenThrow(new RuntimeExceptionWithoutStackTrace("failed")); + ServiceCombException exception = Assertions.assertThrows(ServiceCombException.class, + () -> SwaggerUtils.parseSwagger(url)); + Assertions.assertTrue(exception.getMessage().contains("Parse swagger from url failed, ")); + } } @Test diff --git a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/TestSwaggerUtils.java b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/TestSwaggerUtils.java index 0be165b3da0..4d3d1049766 100644 --- a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/TestSwaggerUtils.java +++ b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/TestSwaggerUtils.java @@ -51,7 +51,6 @@ import io.swagger.models.properties.Property; import io.swagger.models.properties.RefProperty; import io.swagger.models.properties.StringProperty; -import mockit.Expectations; public class TestSwaggerUtils { @@ -259,13 +258,8 @@ public void testInvalidResponseHeader() { @Test public void noParameterName() { Method method = ReflectUtils.findMethod(Schema.class, "testint"); - Parameter parameter = method.getParameters()[0]; - new Expectations(parameter) { - { - parameter.isNamePresent(); - result = false; - } - }; + Parameter parameter = Mockito.spy(method.getParameters()[0]); + Mockito.when(parameter.isNamePresent()).thenReturn(false); IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, () -> SwaggerGeneratorUtils.collectParameterName(parameter));