我在控制器中有一个方法:
@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,但它没有用。
答案 0 :(得分:1)
尝试放
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;