Methods to test Spring application are not found

Asked

Viewed 104 times

1

I am 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 it is not finding the methods get(), status() and content(), where these methods are imported?

1 answer

3


Try to exchange imports for 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")));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.