The Recordo MockMvc module empowers developers to streamline the testing of controllers using the MockMvc framework, offering two powerful mechanisms for testing flexibility:
Developers can define an interface where they specify the API endpoints they want to test using standard Spring annotations. This intuitive approach simplifies the testing process, as Recordo handles the underlying logic. Developers only need to autowire this interface in their test classes.
Developers can declare necessary requests directly as parameters in their test methods, specifying the HTTP method, URI, request body, and other details. This approach provides fine-grained control over requests.
Annotation Parameters
Usage Examples
@TestvoidshouldGetBooks( @Get("/users/1/books?page=2") Page<Book> books) {// Perform test assertions using the retrieved books// ...}@TestvoidshouldGetNotFound( @Get(value ="/users/1/books?page=2", expectedStatus =HttpStatus.NOT_FOUND) ErrorDto errorDto) {// ...}@TestvoidshouldCreateBook( @Post(value ="/books", expectedStatus =HttpStatus.CREATED) Request<Book> request) {// Create a book instanceBook book =...// Perform the request and retrieve the responseResponse<Book> response =request.body(book).perform();Book createdBook =response.getBody();// Perform test assertions using the created book// ...}@TestvoidshouldUpdateBook( @Put(value ="/books", body = @Content(file ="/book.json")) Book updatedBook) {// Perform test assertions using the updated book// ...}@TestvoidshouldDeleteBook( @Delete(value ="/books/{id}", expectedStatus =HttpStatus.NO_CONTENT) Response<Void> response) {// Perform test assertions for successful deletion// ...}
Type Options
Available in both the API client interfaces and when declaring requests as test parameters
Developers have flexibility in choosing how they handle response and request types. Result Type Options offer a convenient way to interact with API responses for diverse testing scenarios.
1. Object Result Type (Page<Book>, Book)
Developers can directly retrieve the response body as an object, simplifying test assertions.