MockServer Module

RECORDO

During the first test run or in the absence of a file, all real interactions are recorded into a file.

Once the file is saved, it is automatically utilized for mocking purposes.

Setup

HTTP Interceptor

Recordo adds an interceptor to your HTTP client to capture and replay HTTP requests and responses.

At present, three HTTP clients are supported:

  • Spring RestTemplate

  • OkHttp Client

  • Apache HTTP Client

Recordo searches for an HTTP client in the application context. If you are not using the Spring Framework or if you don't have an HTTP client or have multiple ones, you can specify which HTTP client to use by explicitly adding the @EnableRecordo annotation.

If you need to use multiple MockServers for different HTTP clients, you can specify the HTTP client bean or the name of the test class field in the `httpClient` annotation parameter.

@Autowired
@EnableRecordo
private RestTemplate restTemplate;

OpenFeign Client definition example

You can use an HTTP client that has been intercepted underneath OpenFeign or other high-level clients.

@Bean
public okhttp3.OkHttpClient okHttpClient() {
    return new okhttp3.OkHttpClient();
}

@Bean
public feign.Client feignClient(OkHttpClient okHttpClient) {
    return new feign.okhttp.OkHttpClient(okHttpClient);
}

Annotation Parameters

The @MockServer annotation has 4 parameters:

NameTypeDescription

value

String

Path to JSON file

urlPattern

String

The mapping matches URLs using the following rules: ? matches one character *matches zero or more characters ** matches zero or more directories in a path

httpClient

String

Name of RestTemplate, OkHttp, or Apache HTTP Client bean or test class field

objectMapper

String

Name of ObjectMapper bean or test class field

Examples

Single Server

@Test
@MockServer("/mock_servers/get_gists.json")
void should_retrieve_gists() {
    ...
    List<GistResponse> gists = gitHubClient.getGists();
    ...
}
@Test
@MockServer("/mock_servers/create_and_delete_gist.json")
void should_create_and_delete_gist() {
    ...
    GistResponse response = gitHubClient.createGist(gist);
    Gist created = gitHubClient.getGist(response.getId());
    gitHubClient.deleteGist(response.getId());
    ...
}

Multiple HTTP clients

@Autowired
private RestTemplate bookServerRestTemplate;

@Autowired
private RestTemplate authorServerRestTemplate;

@Test
@MockServer(httpClient = "bookServerRestTemplate", value = "/mockserver/multiservers/books-server.rest.json")
@MockServer(httpClient = "authorServerRestTemplate", value = "/mockserver/multiservers/authors-server.rest.json")
void should_retrieve_books() {
    ...
    List<Book> allBooks = restClient.get("https://books.server/books", listOf(Book.class));
    List<Author> allAuthors = restClient.get("https://authors.server/authors", listOf(Author.class));
    Book book = restClient.get("https://books.server/books/129649986932158", typeOf(Book.class));
    Author author = restClient.get("https://authors.server/authors/1", typeOf(Author.class));
    ...
}

Multiple Servers

@Test
@MockServer(urlPattern = "https://books.server/**", value = "/mockserver/multiservers/books-server.rest.json")
@MockServer(urlPattern = "https://authors.server/**", value = "/mockserver/multiservers/authors-server.rest.json")
void should_retrieve_books() {
    ...
    List<Book> allBooks = restClient.get("https://books.server/books", listOf(Book.class));
    List<Author> allAuthors = restClient.get("https://authors.server/authors", listOf(Author.class));
    Book book = restClient.get("https://books.server/books/129649986932158", typeOf(Book.class));
    Author author = restClient.get("https://authors.server/authors/1", typeOf(Author.class));
    ...
}

Last updated