Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do I have to import for SpringMVC3.2 unit testing?

I am writing SpringMVC3.2 unit test and I imported classes:

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

this.mockMvc.perform(post("/maps.do").accept(MediaType.APPLICATION_JSON)
                .param("mobileno", "111111")
                .param("fromdate", "22-02-2013").param("fromtime", "07:37")
                .param("todate", "22-02-2013").param("totime", "08:17")
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(content().mimeType(MediaType.APPLICATION_JSON)
        .andExpect(jsonPath("$.name").value("Lee"));

But print() and mimeType() still are not resolved. I have been searching but I could not find correct answer.

Thanks.

EDITED I tried to

import static org.springframework.test.web.server.result.MockMvcResultHandlers.print;

but it could not be resolved. What library am I missing and dependency do I add? Above is misspell and below is correct.

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
like image 749
sunghun Avatar asked Mar 07 '13 06:03

sunghun


People also ask

Which class is used to test Spring MVC REST Web application?

We create the test data by using a test data builder class. Configure our mock object to return the created test data when its findAll() method is invoked.

What is MockMvc used for?

MockMvc provides support for Spring MVC testing. It encapsulates all web application beans and makes them available for testing. We'll initialize the mockMvc object in the @BeforeEach annotated method, so that we don't have to initialize it inside every test.

What is MockMvcBuilders standaloneSetup?

The MockMvcBuilders. standaloneSetup() allows to register one or more controllers without the need to use the full WebApplicationContext .

What is webAppContextSetup?

public static DefaultMockMvcBuilder webAppContextSetup(WebApplicationContext context) Build a MockMvc instance using the given, fully initialized (i.e., refreshed) WebApplicationContext . The DispatcherServlet will use the context to discover Spring MVC infrastructure and application controllers in it.


1 Answers

org.springframework.test.web.servlet.result.MockMvcResultHandlers

Comes from spring-test-3.2.3.RELEASE.jar in my workspace

like image 77
user2687058 Avatar answered Sep 18 '22 15:09

user2687058