我在控制器中有一个方法:
@RequestMapping(value = "/", method = RequestMethod.GET)
public final String mainGet(final HttpServletRequest request,
final HttpServletResponse response, ModelMap model)
throws ServletException {
model.addAttribute("fontSize", fontSize);
courses = persistenceServices.getCourses();
model.addAttribute("coursesList", courses);
return "home";
}我写了一个简单的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:tests.xml" })
@WebAppConfiguration
public class TestHomeController {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Mock
private PersistenceServices persistenceServicesMock;
@InjectMocks
private HomeController homeController;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(homeController).build();
}
@Test
public void testMainGet() throws Exception {
mockMvc.perform( get("/")).andExpect(status().isOk()).andExpect(view().name("home"));
verify(persistenceServicesMock, times(1));
verifyNoMoreInteractions(persistenceServicesMock);
}一切似乎都很好,但我不能调用任何MvcMock的方法。Eclipse下划线了所有它们(执行、状态、查看等)并向我显示信息:
The method perform(RequestBuilder) in the type MockMvc is not applicable for the arguments (MockHttpServletRequestBuilder)我试图将参数传递给RequestBuilder和ResultMatcher,但不起作用。
发布于 2015-10-26 15:36:28
试着把
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;https://stackoverflow.com/questions/21480508
复制相似问题