An example test for your controller can be something as simple as
1 | public class DemoApplicationTests { |
The new testing improvements that debuted in Spring Boot 1.4.M2 can help reduce the amount of code you need to write situation such as these.
The test would look like so:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16@RunWith(SpringRunner.class)
@WebMvcTest(HelloWorld.class)
public class UserVehicleControllerTests {
@Autowired
private MockMvc mvc;
@Test
public void testSayHelloWorld() throws Exception {
this.mockMvc.perform(get("/").accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"));
}
}
还有一种方式是使用TestRestTemplate
1 | package controller; |