Methods for testing Spring application are not found

1

I'm following the following article: Introduction To Spring MVC Test Framework

I have the following code:

this.mockMvc.perform(get("/product/1"))
.andExpect(status().isOk().
.alwaysExpect(content().contentType("application/json;charset=UTF-8")));

And the class has these notes:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = "classpath*:/spring/spring*.xml")

However, you are not finding the methods get() , status() and content() , where are these methods imported from?

    
asked by anonymous 18.02.2016 / 12:16

1 answer

3

Try to switch imports to these in this order:

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

and correct after isOk ()

this.mockMvc.perform(get("/product/1"))
.andExpect(status().isOk())
.alwaysExpect(content().contentType("application/json;charset=UTF-8")));
    
18.02.2016 / 12:38