4

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"));
    }
}
Adam Bogdan Boczek
  • 1,720
  • 6
  • 21
  • 34
baymon
  • 459
  • 1
  • 5
  • 20
  • I recommend you using *Organize imports* feature, every IDE has it. For example in IntelliJ is Ctrl + Alt + O. http://stackoverflow.com/a/8609200/2757140 – Piotr Lewandowski Jan 24 '17 at 19:02
  • I'm searching also for `given` import. – zolv Aug 11 '20 at 08:06
  • OK, got it, it's part of `BDDMockito` class. 2h to find it. – zolv Aug 11 '20 at 10:14
  • 6.5 years later and SpringBoot folks still leaving off import statements in their documentation. I'm still having terrible time finding imports for these commands. – tlarson Jul 23 '23 at 19:02

2 Answers2

10

I already found out:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
baymon
  • 459
  • 1
  • 5
  • 20
4

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;
Community
  • 1
  • 1
Avinash
  • 4,115
  • 2
  • 22
  • 41