Integration testing

The code to perform integration testing on the TodoController class is shown in the following code snippet. It launches up the entire Spring context with all the controllers and beans defined:

   @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment =
SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TodoControllerIT {

@LocalServerPort
private int port;

private TestRestTemplate template = new TestRestTemplate();

@Test
public void retrieveTodos() throws Exception {
String expected = "["
+ "{id:1,user:Jack,desc:"Learn Spring MVC",done:false}" + ","
+ "{id:2,user:Jack,desc:"Learn Struts",done:false}" + "]";

String uri = "/users/Jack/todos";

ResponseEntity<String> response =
template.getForEntity(createUrl(uri), String.class);

JSONAssert.assertEquals(expected, response.getBody(), false);
}

private String createUrl(String uri) {
return "http://localhost:" + port + uri;
}
}

This test is very similar to the integration test for BasicController, except that we are using JSONAssert to assert the response.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset