本人使用的spring为4.0的,在进行测试时,报如下错误:
Bound mismatch: The generic method webAppContextSetup(WebApplicationContext) of type MockMvcBuilders is not applicable for the arguments (WebApplicationContext). The inferred type DefaultMockMvcBuilder<DefaultMockMvcBuilder<B>> is not a valid substitute for the bounded parameter <B extends DefaultMockMvcBuilder<B>>
以下是本人的测试代码,坐等解决,万分谢谢
import org.mockito.Matchers;
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.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration(value = “src/main/webapp”)
@ContextHierarchy({
@ContextConfiguration(name = “parent”, locations = “classpath:spring-config.xml”),
@ContextConfiguration(name = “child”, locations = “classpath:spring-mvc.xml”)
})
public class UserControllerWebAppContextSetupTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setUp() {
//在这里报的错误。
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void testView() throws Exception {
mockMvc .perform(get(“/user/1”)) .andDo(print());
}
}
Bound mismatch: The generic method webAppContextSetup(WebApplicationContext) of type MockMvcBuilders is not applicable for the arguments (WebApplicationContext). The inferred type DefaultMockMvcBuilder<DefaultMockMvcBuilder<B>> is not a valid substitute for the bounded parameter <B extends DefaultMockMvcBuilder<B>>
以下是本人的测试代码,坐等解决,万分谢谢
import org.mockito.Matchers;
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.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration(value = “src/main/webapp”)
@ContextHierarchy({
@ContextConfiguration(name = “parent”, locations = “classpath:spring-config.xml”),
@ContextConfiguration(name = “child”, locations = “classpath:spring-mvc.xml”)
})
public class UserControllerWebAppContextSetupTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setUp() {
//在这里报的错误。
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void testView() throws Exception {
mockMvc .perform(get(“/user/1”)) .andDo(print());
}
}
解决方案