Mock external server during integration testing with Spring Mock external server during integration testing with Spring spring spring

Mock external server during integration testing with Spring


After playing a bit with various scenarios, here is the one way how can one achieve what was asked with minimal interventions to the main code

  1. Refactor your controller to use a parameter for thirdparty server address:

    @RestControllerpublic class HelloController {    @Value("${api_host}")    private String apiHost;    @RequestMapping("/hello_to_facebook")    public String hello_to_facebook() {        // Ask facebook about something        HttpGet httpget = new HttpGet(buildURI("http", this.apiHost, "/oauth/access_token"));        String response = httpClient.execute(httpget).getEntity().toString();        // .. Do something with a response        return response + "_PROCESSED";    }}

'api_host' equals to 'graph.facebook.com' in application.properties in the src/main/resources

  1. Create a new controller in the src/test/java folder that mocks the thirdparty server.

  2. Override 'api_host' for testing to 'localhost'.

Here is the code for steps 2 and 3 in one file for brevity:

@RestControllerclass FacebookMockController {    @RequestMapping("/oauth/access_token")    public String oauthToken() {        return "TEST_TOKEN";    }}@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = Application.class)@WebAppConfiguration@IntegrationTest({"api_host=localhost",})public class TestHelloControllerIT {            @Test    public void getHelloToFacebook() throws Exception {        String url = new URL("http://localhost:8080/hello_to_facebook").toString();        RestTemplate template = new TestRestTemplate();        ResponseEntity<String> response = template.getForEntity(url, String.class);        assertThat(response.getBody(), equalTo("TEST_TOKEN_PROCESSED"));        // Assert that facebook mock got called:        // for example add flag to mock, get the mock bean, check the flag    }}

Is there a nicer way to do this? All feedback is appreciated!

P.S. Here are some complications I encountered putting this answer into more realistic app:

  1. Eclipse mixes test and main configuration into classpath so you might screw up your main configuration by test classes and parameters: https://issuetracker.springsource.com/browse/STS-3882 Use gradle bootRun to avoid it

  2. You have to open access to your mocked links in the security config if you have spring security set up. To append to a security config instead of messing with a main configuration config:

    @Configuration@Order(1)class TestWebSecurityConfig extends WebSecurityConfig {    @Override    protected void configure(HttpSecurity http) throws Exception {        http            .authorizeRequests()                .antMatchers("/oauth/access_token").permitAll();        super.configure(http);    }}
  3. It is not straightforward to hit https links in integration tests. I end up using TestRestTemplate with custom request factory and configured SSLConnectionSocketFactory.


If you use RestTemplate inside the HelloController you would be able to test it MockRestServiceTest, like here: https://www.baeldung.com/spring-mock-rest-template#using-spring-test

In this case

@RunWith(SpringJUnit4ClassRunner.class)// Importand we need a working environment@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)public class TestHelloControllerIT {        @Autowired    private RestTemplate restTemplate;    // Available by default in SpringBootTest env    @Autowired    private TestRestTemplate testRestTemplate;    @Value("${api_host}")    private String apiHost;    private MockRestServiceServer mockServer;    @Before    public void init(){        mockServer = MockRestServiceServer.createServer(this.restTemplate);    }    @Test    public void getHelloToFacebook() throws Exception {        mockServer.expect(ExpectedCount.manyTimes(),            requestTo(buildURI("http", this.apiHost, "/oauth/access_token"))))            .andExpect(method(HttpMethod.POST))            .andRespond(withStatus(HttpStatus.OK)                    .contentType(MediaType.APPLICATION_JSON)                    .body("{\"token\": \"TEST_TOKEN\"}")            );        // You can use relative URI thanks to TestRestTemplate        ResponseEntity<String> response = testRestTemplate.getForEntity("/hello_to_facebook", String.class);        // Do the test you need    }}

Remember that you need a common RestTemplateConfiguration for autowiring, like this:

@Configurationpublic class RestTemplateConfiguration {    /**     * A RestTemplate that compresses requests.     *     * @return RestTemplate     */    @Bean    public RestTemplate getRestTemplate() {        return new RestTemplate();    }}

And that you have to use it inside HelloController as well

@RestControllerpublic class HelloController {    @Autowired    private RestTemplate restTemplate;    @RequestMapping("/hello_to_facebook")    public String hello_to_facebook() {        String response = restTemplate.getForEntity(buildURI("https", "graph.facebook.com", "/oauth/access_token"), String.class).getBody();        // .. Do something with a response        return response;    }}


2018 Things have improved much.I ended up using spring-cloud-contracts Here's a video introduction https://www.youtube.com/watch?v=JEmpIDiX7LU . The first part of the talk walk you through a legacy service. That's the one you can use for external API.

Gist is,

  • You create a Contract for the external service using Groovy DSL or other methods that even support explicit calls/proxy or recording. Check documentation on what works for you

  • Since you dont actually have control over the 3rd party in this case, you will use the contract-verifier and create the stub locally but remember to skipTests

  • With the stub-jar now compiled and available you can run it from within your test cases as it will run a Wiremock for you.

This question and several stackoverflow answers helped me find the solution so here is my sample project for the next person who has these and other similar microservices related tests.

https://github.com/abshkd/spring-cloud-sample-games

With everything working once you will never ever look back and do all your tests with spring-cloud-contracts

@marcin-grzejszczak the author, is also on SO and he helped a lot figure this out. so if you get stuck, just post on SO.