我正在尝试使用Spring测试来测试我的Spring MVC webapp.它使用springmvc-router进行路由,这似乎打破了测试,当我使用@RequestMapping而不是我的routes.conf文件时,它可以正常工作.
我有一个.jsp名为的文件valid.jsp,当我从Jetty运行开发站点时,它显示正常.控制器是:
@Controller
@EnableWebMvc
public class AuthController {
public String valid() {
return "valid";
}
}
Run Code Online (Sandbox Code Playgroud)
我的routes.conf文件映射GET /valid authController.valid.
现在,我的测试仪看起来像
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/test-context.xml",
"/spring/spring-security.xml",
"file:src/main/webapp/WEB-INF/mvc-config.xml"})
@WebAppConfiguration
@Import(RouteConfig.class)
public class AuthControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private AuthenticationManager authenticationManager;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
mockMvc =
MockMvcBuilders.webAppContextSetup(webApplicationContext).dispatchOptions(true).build();
}
@Test
public void testValid() throws Exception {
mockMvc.perform(get("/validation-success"))
.andDo(print())
.andExpect(status().isOk());
} …Run Code Online (Sandbox Code Playgroud)