Produces a mock for REST controllers and REST clients in the Spring Boot framework
In your pom.xml add the following dependency:
<dependency>
<groupId>io.github.allanmarques83</groupId>
<artifactId>spring-mock-rest</artifactId>
<version>1.1.5</version>
</dependency>
1 . Apply the annotation @EnableRestMock in your SpringBoot application class. E.g:
@SpringBootApplication
@EnableRestMock
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2 . Add a .json file (fake json response body) in the resources path of spring framework.
3 . In your Rest controller or client class, add the annotation below in the desired method. E.g:
@GetMapping("/test")
@MockRest(mockId="123456", response="your_file.json", statusCode=400)
public ResponseEntity<String> someMethod(@RequestParam(required=false) String mockId) {
return ResponseEntity.ok("real response");
}
If you call the endpoint service: /test?mockId=123456 the response is gonna be your mock with the http status code 400.
@MockRestValues({
@MockRest(mockId="testFake1", response="mockrest/test_fake1.json", statusCode=400),
@MockRest(mockId="testFake2", response="mockrest/test_fake2.json", statusCode=401)
})
public ResponseEntity<String> someMethod(@RequestParam(required=false) String mockId) {
return ResponseEntity.ok("real response");
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.