Skip to content

Commit

Permalink
migrate generator-core to mockito (#3424)
Browse files Browse the repository at this point in the history
  • Loading branch information
fu-turer authored Oct 23, 2022
1 parent df9b7c3 commit 870dbdc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 42 deletions.
6 changes: 3 additions & 3 deletions swagger/swagger-generator/generator-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
</dependency>

<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<scope>provided</scope>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<IOUtils> 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<IOUtils> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 870dbdc

Please sign in to comment.