You can use the Wiremock framework as a mock server to run integration tests without accessing the external service. You can mock external services, such as payment gateways, payment providers, and more.
Adding New Mock Services
About this Task
You can create new mock services from the available implementation patterns.
- Create a mock service by performing the following:
- Go to ext-mock-examples.
- Copy the example into the ext-mock-services module.
- Modify the example as required.
- Create a new service mapping file.
Note: Ensure that the mapping is saved a directory matching the service name in the ext-mock-services/src/main/resources/wiremock/mappings/ module.
Configuring Static Mappings and Responses
About this Task
You can configure a mock service with static mappings and response files. For more information about configuring static mapping, see the ext-mock-examples/src/main/resources/wiremock/mappings/static directory.
- Create a static mapping.
- In the ext-mock-services/src/main/resources/wiremock
directory, add the static response files in a directory matching the service
For example, ext-mock-services/src/main/resources/wiremock/<directory_name>/files. For more information about mapping, see the ext-mock-examples/src/main/resouces/wiremock/<name>files/static directory.Note: You can select response files using templates. For more information about response files and an example, see the ext-mock-examples/src/main/resources/wiremock/mappings/staticByPath directory. Values from the request can also be dynamically injected into the response body. For more information, see ext-mock-examples/src/main/resources/wiremock/<name>files/staticByPath/response_body_3.json.
Dynamic Transformer Mappings
Additional dynamic mappings require the use of a transformer. The transformer can inject any logic to create the response. For more information about transformer mappings, see the Extending Wiremock section.
Using Test integrations
Running Wiremock as a Webapp
- To start a standalone Wiremock server, in the ext-mock-webapp module, run the following command
`mvn clean tomcat8:run-war`
Running Wiremock from Tests
You can run Wiremock from tests, such as JUnit or Cucumber.
- For Wiremock setup examples to run with a standalone unit test case, see the following test in the ext-mock-examples module:
- For Cucumber integration test examples that can be executed remotely, see the system-tests/cucumber module.
Creating and Configuring a New Dynamic Mapping
- In the ext-mock-services/src/main/java module, create a class in a new
package that extends one of the following:
- In the new package, add a README.md file detailing how the new mock service works and what it does.
- In the ext-mock-webapp/src/main/webapp/WEB-INF/web.xml file, add the fully
qualified name of the class to the transformerClasses servlet context
Note: This field contains a comma-delimited list of all configured transformer extensions.
- In the ext-mock-services/src/main/resources/wiremock/mappings/ directory,
create a service mapping for the new
Note: The mapping must contain a reference to the new transformer class. For more information about service mappings and examples, see the ext-mock-examples/src/main/resources/wiremock/mappings directory. For implementation examples, see the simple, jackson, and soap directories within the mappings directory.
- In the com.elasticpath.wiremock.examples.WireMockExamplesServicesTest test package, in the ext-mock-examples module, view examples of how to create test cases to test the new mock service.
Configuring a Mock URL
About this Task
For running the integration tests against Wiremock, you must update the service outbound endpoints to match your service mappings.
- Go to the commerce-parent pom.xml file.
- In the ep.mock.tomcat.port.http property, modify the port
For example, the following URL is for the healthcheck example service, using the default port:
Note: By default, Wiremock runs on port 8080. If you use the ep-developer profile, the default port is 8086.
Note: You can configure the context root, mock in the ep.mock.context Maven variable as configured in the ep-commerce/pom.xml file.
Mock Service Examples
You can use the examples provided in the ext-mock-examples module as templates for new mock services.
Note: Unit and integration tests that execute against sample services can fail if the services are modified. To make changes, you must copy the example you want to customize in the ext-mock-services module.
The following list describes the available mock services:
- /static: Returns an HTTP 200 response with a static, pre-built json response from a file in __files/static directory.
- /staticByPath: Demonstrates how to use request templating to serve different static responses based on the values in the request. The path, body, cookies, headers, and more, in the request can be interrogated to serve different static responses. For more information about response templating, see Wiremock Response Templating.
- /healthcheck: Returns an HTTP 200 response with no body.
- /simple: Uses a custom transformer to take a value from the request JSON and inject it into the response JSON.
- /jackson: Uses a custom transform, which utilizes Jackson to deserialize a JSON request into POJOs. The request POJOs are used to create a set of response POJOs, which are serialized back into a response JSON with Jackson.
- /soap: Uses a custom transformer, which parses a soap request and generate a soap response using JAXB.