Mushrooms is an easy setup mock or stub framework. To ensure high levels of efficiency for remote service integration.
Remote service integration, especially based on HTTP protocol, e.g. web service, REST etc, highly unstable when developing.
Stub feature rely on spring boot and okhttp3, make sure FeignClient/RestTemplate are using okhttp3.
- Stub REST API.
- Stub Soap API.
- io.github.openfeign:feign-okhttp.
repositories {
jcenter()
}
dependencies {
compile 'com.github.wenhao:mushrooms:3.0.8'
}
<dependency>
<groupId>com.github.wenhao</groupId>
<artifactId>mushrooms</artifactId>
<version>3.0.8</version>
</dependency>
./gradlew clean build
Request Matchers
A request matcher can contain any of the following matchers:
- method - string value as a plain text, regular expression.
- path - string value as a plain text, regular expression.
- query string - key to multiple values as a plain text, regular expression.
- headers - key to multiple values as a plain text, regular expression.
- body
- XPath(example, body: xpath:/Envelope/Body/GetBookRequest[BookName='Java']), without NAMESPACE.
- XML - full or partial match.
- JSON - full or partial match.
- JsonPath(example, body: jsonPath:$.store.book[?(@.price < 10)]), jsonPath syntax
- add application.yml configuration, eg.MushroomsStubConfiguration.java.
- initialize StubOkHttpClientInterceptor via spring bean config, eg.BeanConfiguration.java.
- override the OkHttpFeignConfiguration if you're using FeignClient, eg.OkHttpFeignConfiguration.
Full setup
mushrooms:
stub:
enabled: true
failover: true
stubs:
- request:
path: ${REAL_HOST:http://localhost:8080}/stub(.*)
parameters:
- key: [A-z]{0,10}
value: [A-Z0-9]+
method: P(.*)
headers:
- key: [A-z]{0,10}
value: [A-Z0-9]+
body: /stubs/stub_rest_request.json
response: /stubs/stub_rest_response.json
Enabled mushrooms stub and set stub request and response.
Stub REST API
mushrooms:
stub:
enabled: true
stubs:
- request:
path: ${REAL_HOST:http://localhost:8080}/stub
method: POST
body: /stubs/stub_rest_request.json
response: /stubs/stub_rest_response.json
Stub Soap API
mushrooms:
stub:
enabled: true
stubs:
- request:
path: ${REAL_HOST:http://localhost:8080}/stub/get_book
method: POST
body: /stubs/stub_soap_request.xml
response: /stubs/stub_soap_response.xml
Enabled RestTemplate stub, Customize RestTemplate by using Okhttp3, RestTemplateConfiguration.
@Configuration
public class RestTemplateConfiguration {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory) {
return new RestTemplate(clientHttpRequestFactory);
}
@Bean
public ClientHttpRequestFactory okHttp3ClientHttpRequestFactory(OkHttpClient okHttpClient) {
return new OkHttp3ClientHttpRequestFactory(okHttpClient);
}
}
If enabled okhttp stub, enabling feign okhttp client.
feign:
okhttp:
enabled: true
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: full
Logging
logging:
level:
com.github.wenhao.mushrooms: DEBUG
As Failover is true, will call real endpoint first and return real response if health.
Copyright © 2018-2019 Wen Hao
Licensed under Apache License