【发布时间】:2014-04-22 16:29:33
【问题描述】:
这个 Spring 3 控制器的基本 Spring 测试给了我一个响应代码 404 结果,而不是预期的 200:
@RunWith(SpringJUnit4ClassRunner.class)
public class RootControllerMvcTest extends AbstractContextControllerTests {
private MockMvc mockMvc;
@Before
public void setup() throws Exception {
this.mockMvc = webAppContextSetup(this.wac)
.alwaysExpect(status().isOk()).build();
}
@Test
public void viewIndex() throws Exception {
this.mockMvc.perform(get("/")).andExpect(view()
.name(containsString("index"))).andDo(print());
}
AbstractContextControllerTests:
@WebAppConfiguration("file:src/main/webapp/WEB-INF/spring/webmvc-config.xml")
@ContextConfiguration("file:src/main/resources/META-INF/spring/applicationContext.xml")
public class AbstractContextControllerTests {
@Autowired
protected WebApplicationContext wac; }
我已经通过另一个测试验证了控制器方法本身,但是当我使用上下文时,即使控制器在容器中运行时确实提供了正确的页面,测试也会失败。
有问题的控制器如下所示:
@Controller
public class RootController {
@Autowired
CategoryService categoryService;
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "text/html")
public String index(Model uiModel) {
uiModel.addAttribute("categories", categoryService.findAll());
return "index";
}
显然,我不是在测试我的想法。有什么建议如何对这个问题进行三角测量?
我在Pastebin 上发布了完整的 web mvc 文件,以免把所有空间都弄得乱七八糟。
【问题讨论】:
标签: java spring-3 spring-test