Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC test with Spring Boot 1.4 imports for get(), status() and content()

This blog describes some of the test improvements in Spring Boot 1.4. Unfortunately it seems that some important informations are missing. What static import is required to use the methods get(), status() and content() from the following example?

@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"));
    }
}
like image 407
baymon Avatar asked Sep 05 '25 16:09

baymon


2 Answers

I already found out:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
like image 67
baymon Avatar answered Sep 07 '25 17:09

baymon


You can use the following guide to use auto import eclipse feature for static import.

Eclipse Optimize Imports to Include Static Imports

The Exact answer to your question is following.

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
like image 22
Avinash Avatar answered Sep 07 '25 16:09

Avinash