12

In my application I have a lot of REST- Services. I have written tests for all services with:

org.springframework.web.client.RestTemplate

A REST- Service invokation e.g. looks like this:

final String loginResponse = restTemplate.exchange("http://localhost:8080/api/v1/xy", HttpMethod.POST, httpEntity, String.class)
        .getBody();

and I check the response body afterwards - all works fine. The disadvantage is, that the application for sure must be started in order to invoke the REST- Services.

My question now would be how I can do this in my JUnit- @Test methods? It is a Spring Boot application (with embedded tomcat).

Thanks for help!

Raniz
  • 10,882
  • 1
  • 32
  • 64
quma
  • 5,233
  • 26
  • 80
  • 146

4 Answers4

17

There's a good chapter on this in the documentation, I suggest you read through it to fully understand what you can do.

I like to use @IntegrationTest with a custom configuration since that starts up the entire server and lets you test the complete system. If you want to replace certain parts of the system with mocks you can do that by excluding certain configurations or beans and replacing them with your own.

Here's a small example. I've left out the MessageService interface because it's obvious from IndexController what it does, and it's default implementation - DefaultMessageService - because it's not relevant.

What it does is that it spins up the entire application minus the DefaultMessageService but with it's own MessageService instead. It then uses RestTemplate to issue real HTTP requests to the running application in the test case.

Application classes:

IntegrationTestDemo.java:

@SpringBootApplication
public class IntegrationTestDemo {

    public static void main(String[] args) {
        SpringApplication.run(IntegrationTestDemo.class, args);
    }

}

IndexController.java:

@RestController
public class IndexController {

    @Autowired
    MessageService messageService;

    @RequestMapping("/")
    String getMessage() {
        return messageService.getMessage();
    }
}

Test classes:

IntegrationTestDemoTest.java:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfig.class)
@WebIntegrationTest // This will start the server on a random port
public class IntegrationTestDemoTest {

    // This will hold the port number the server was started on
    @Value("${local.server.port}")
    int port;

    final RestTemplate template = new RestTemplate();

    @Test
    public void testGetMessage() {
        String message = template.getForObject("http://localhost:" + port + "/", String.class);

        Assert.assertEquals("This is a test message", message);
    }
}

TestConfig.java:

@SpringBootApplication
@ComponentScan(
    excludeFilters = {
        // Exclude the default message service
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = DefaultMessageService.class),
        // Exclude the default boot application or it's
        // @ComponentScan will pull in the default message service
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = IntegrationTestDemo.class)
    }
)
public class TestConfig {

    @Bean
    // Define our own test message service
    MessageService mockMessageService() {
        return new MessageService() {
            @Override
            public String getMessage() {
                return "This is a test message";
            }
        };
    }
}
Raniz
  • 10,882
  • 1
  • 32
  • 64
9

If you were not looking for a end to end (integretion) test, the MockRestServiceServer might help you. I found it's very useful to de-couple my test cases from a real service.

Spring doc said:

Used for tests that involve direct or indirect use of the RestTemplate. Provides a way to set up expected requests that will be performed through the RestTemplate as well as mock responses to send back thus removing the need for an actual server.

Here is the official doc


One more tip is that, requestTo can not be imported automatically

server.expect(manyTimes(), requestTo("/hotels/42")) ....

It's a static method of org.springframework.test.web.client.match.MockRestRequestMatchers

simomo
  • 706
  • 10
  • 24
  • This works perfectly for testing that the Dto you wrote maps to the expected Json you will get from the rest service. Which is pretty much the use case for testing RestTemplate usage. – JeeBee May 24 '17 at 17:31
7

Since you are using Spring MVC for REST, I would recommend using the testing facilities supplied by instantiating MockMVC() - enabling tests such as:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {
 ... // any required Spring config
)
@WebAppConfiguration
public class RestControllerTest {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }


    @Test
    public void getUserList() throws Exception {
        mockMvc.perform(get("/user"))
            .andExpect(status().isOk())
            .andExpect(content().contentType("application/json;charset=UTF-8")) 
            .andExpect(content().encoding("UTF-8"))
            .andExpect(jsonPath("$", hasSize(8)))
            .andExpect(jsonPath("$[0].id").exists())
            .andExpect(jsonPath("$[0].alias").exists())
            .andExpect(jsonPath("$[0].name").exists())
        );
    }
}

This unit test will test a REST interface without deploying. Specifically, whether exactly 8 users are returned and the first one has the fields 'id', 'alias' and 'name'.

The jsonPath assertions require two dependencies:

'com.jayway.jsonpath:json-path:0.8.1'
'com.jayway.jsonpath:json-path-assert:0.8.1'

And probably also:

'org.springframework:spring-test:4.1.7.RELEASE'
morsor
  • 1,263
  • 14
  • 29
  • This doesn't really answer the question since it doesn't show how to set up `MockMVC`. You should either post a complete answer or direct questions to OP with a comment. – Raniz Jul 31 '15 at 08:16
  • 2
    I've updated the answer and agree that a comment was more fitting. However, since my comment required posting some code, I chose to make it an incomplete answer that I would either remove or expand on. – morsor Jul 31 '15 at 08:30
  • Is it possible to deserialize the JSON into a Java object using MockMvc? – Jordão Jul 19 '16 at 02:54
  • May I ask what the purpose of that would be? – morsor Jul 19 '16 at 14:32
  • @Jordão Yes. Just call andReturn() method at the end of the chain calls to get a MvcResult object. Than you can use mvcResult.getResponse.getContentAsString() and deserialize the JSON using your unmarshaller. – Daniel Jun 23 '17 at 16:21
1

If you use Spring Boot, you can easily setup everything to test your RestTemplate if you annotate your test with @RestClientTest. This ensures to auto-configure the required parts (RestTemplateBuilder, ObjectMapper, MockRestServiceServer, etc.) of your application to test your client classes like e.g.:

@Component
public class UserClient {

  private final RestTemplate restTemplate;

  public UserClient(RestTemplateBuilder restTemplateBuilder) {
    this.restTemplate = restTemplateBuilder.rootUri("https://reqres.in").build();
  }

  public User getSingleUser(Long id) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<Void> requestEntity = new HttpEntity<>(headers);

    return this.restTemplate
      .exchange("/api/users/{id}", HttpMethod.GET, requestEntity, User.class, id)
      .getBody();

  }
}

The corresponding test (using JUnit 5) looks like the following:

@RestClientTest(UserClient.class)
class UserClientTest {

  @Autowired
  private UserClient userClient;

  @Autowired
  private ObjectMapper objectMapper;

  @Autowired
  private MockRestServiceServer mockRestServiceServer;

  @Test
  public void userClientSuccessfullyReturnsUserDuke() throws Exception {

    String json = this.objectMapper
      .writeValueAsString(new User(new UserData(42L, "duke@java.org", "duke", "duke", "duke")));

    this.mockRestServiceServer
      .expect(requestTo("/api/users/42"))
      .andRespond(withSuccess(json, MediaType.APPLICATION_JSON));

    User result = userClient.getSingleUser(42L);

    assertEquals(42L, result.getData().getId());
    assertEquals("duke", result.getData().getFirstName());
    assertEquals("duke", result.getData().getLastName());
    assertEquals("duke", result.getData().getAvatar());
    assertEquals("duke@java.org", result.getData().getEmail());
  }

}

This setup allows you to specify stub HTTP responses using MockRestServiceServer.

I've provided a more detailed tutorial for this, if wan to learn more about it.

rieckpil
  • 10,470
  • 3
  • 32
  • 56