使用 Spring Boot 1.4 导入 get()、status() 和 content() 的 MVC 测试

bay*_*mon 1 testing spring unit-testing spring-mvc spring-boot

这篇博客描述了 Spring Boot 1.4 中的一些测试改进。不幸的是,似乎缺少一些重要信息。什么静态导入需要使用的方法get()status()content()从下面的例子吗?

@RunWith(SpringRunner.class)
@WebMvcTest(UserVehicleController.class)
public class UserVehicleControllerTests {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private UserVehicleService userVehicleService;

    @Test
    public void getVehicleShouldReturnMakeAndModel() {
        given(this.userVehicleService.getVehicleDetails("sboot"))
            .willReturn(new VehicleDetails("Honda", "Civic"));

        this.mvc.perform(get("/sboot/vehicle")
            .accept(MediaType.TEXT_PLAIN))
            .andExpect(status().isOk())
            .andExpect(content().string("Honda Civic"));
    }
}
Run Code Online (Sandbox Code Playgroud)

bay*_*mon 7

我已经发现:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
Run Code Online (Sandbox Code Playgroud)