我正在尝试使用 junit 测试我的应用程序。
因此我设置了以下课程:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/META-INF/spring/applicationContext-test.xml" )
@TransactionConfiguration
@Transactional
public class DispatcherServletTest extends AbstractJUnit4SpringContextTests {
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private DispatcherServlet dispatcher;
@Before
public void setUp() throws Exception {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
MockServletConfig config = new MockServletConfig("myapp");
config.addInitParameter("contextConfigLocation","classpath*:webmvc-config.xml");
dispatcher = new DispatcherServlet();
dispatcher.init(config);
}
//test cases
}
所以问题是,我的调度程序 servlet 似乎无法向我的任何控制器发送任何请求。
我认为配置中有一些东西 - contextConfiguration Location。
看起来他可以找到该文件(否则会抛出异常),但没有加载任何配置
记录器说:
org.springframework.web.servlet.PageNotFound - 未找到带有 URI [http://localhost:8080/myapp/abc] 的 HTTP 请求的映射
但我完全不知道出了什么问题......
我将不胜感激任何帮助!
提前致谢
我的矿井工作正常,请尝试以下调整。
- 如果您使用 Junit4,则无需扩展测试类,junit 运行程序应该可以解决问题
-
通过类路径加载上下文配置,并确保可以从测试类路径访问
@ContextConfiguration(locations={"classpath:applicationContext-test.xml"})
然后只需测试带注释的控制器。我这样做:
@Test
@Transactional
public void testAnnotatedListUser() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
AnnotationMethodHandlerAdapter handlerAdpt = new AnnotationMethodHandlerAdapter();
request.setRequestURI("/you/URIhere");
ModelAndView mav = handlerAdpt.handle(request, response, this.controller);
assertEquals("Incorrect view name returned", "myexpectedviewname", mav.getViewName());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)