A Maven plugin to create source code to build mocks for Wiremock (https://github.com/tomakehurst/wiremock) framework based on Swagger (https://github.com/swagger-api) spec.
Add plugin to your pom.xml file.
<build>
<plugins>
<plugin>
<groupId>com.github.valentinkarnaukhov</groupId>
<artifactId>mockgen-maven-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<executions>
<execution>
<id>generate-stubs</id>
<goals>
<goal>generate-stubs</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/test/resources/test_swagger_2_0.yaml</inputSpec>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run build
mvn clean install
inputSpec
- Swagger spec file pathmodelPackage
- package of existing models or output packagegenerateModels
- flag to generate models or use existingstubPackage
- out package for stubssupportPackage
- package for support filesoutputDir
- target output pathdelegateObject
- object to wrap default logic of mocks creationconfigOptions.explode
- explode mode on/offconfigOptions.useTags
- if this parameter is true then Tags will be used for class namesconfigOptions.maxDepth
- max depth for explosionconfigOptions.prefixMap
- map for query/body/response prefixes
See example of full configuration there: https://github.com/ValentinKarnaukhov/mockgen-example/blob/master/pom.xml
After plugin execution in target folder will appear *Mock classes. By default generated file contains
- query section (default prefix "inQuery")
- body section (default prefix "inBody")
- response section (default prefix "inResp")
- code section (code*, e.g. code200(), code400()...)
- mock() method
- buildStub() method
@Test
public void allQueryParamsMatchShouldReturn200() throws ApiException {
QueryParamsGetMock.GetObjectByParams mock = new QueryParamsGetMock.GetObjectByParams(); //created code
mock.inQueryStingParam("STRING")
.inQueryIntegerParam(123)
.inQueryLongParam(123L)
.inQueryFloatParam(123.123f)
.inQueryDoubleParam(123.123)
.inQueryBoolParam(true)
.inQueryEnumParam("EV1")
.code200()
.inResp("MATCHED")
.mock();
String response = getApi.getObjectByParams("STRING", 123, 123L, true,
123.123f, 123.123, "EV1"); //client from swagger
Assertions.assertEquals(200, getApi.getApiClient().getStatusCode());
Assertions.assertEquals("MATCHED", response
}
By default mocks will be created in WireMock instance, but it can be changed via <delegateObject>
section.
Target file should contains java class with static public method mock(StubMapping stub)
package com.github.valentinkarnaukhov.mockgenexample;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
public class CustomWiremock {
public static void mock(StubMapping stub) {
System.out.println("Hello: " + stub);
}
}
To use this class during mock creation add this to configuration
<delegateObject>com.github.valentinkarnaukhov.mockgenexample.CustomWiremock</delegateObject>