Skip to content

allanmarques83/spring-mock-rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-mock-rest

Produces a mock for REST controllers and REST clients in the Spring Boot framework

Installation

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>

Usage:

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.

Multiple mocks for the same REST operation:

@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");
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

GPL-3.0

About

Produces a mock for REST controllers and REST clients

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages