Dav*_*vid 50 java spring unit-testing annotations maven
在我们的项目中,我们正在编写测试以检查控制器是否返回正确的模型视图
@Test
public void controllerReturnsModelToOverzichtpage()
{
ModelAndView modelView = new ModelAndView();
KlasoverzichtController controller = new KlasoverzichtController();
modelView = controller.showOverzicht();
assertEquals("Klasoverzichtcontroller returns the wrong view ", modelView.getViewName(), "overzicht");
}
Run Code Online (Sandbox Code Playgroud)
这将返回异常null.
我们现在正在配置@contextconfiguration但是我们不知道如何加载位于src\main\webapp\root\WEB-INF\root-context.xml的正确的xml
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestOverzichtSenario{
....
Run Code Online (Sandbox Code Playgroud)
关于如何确保contextannotation加载正确的xml的任何建议?
编辑v2
我将配置.xml文件从webINF文件夹复制到
src\main\resources\be\..a bunch of folders..\configuration\*.xml
Run Code Online (Sandbox Code Playgroud)
并将webinf中的web.xml更改为
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:configuration/root-context.xml
classpath*:configuration/applicationContext-security.xml
</param-value>
Run Code Online (Sandbox Code Playgroud)
现在得到错误
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)
org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
javax.servlet.GenericServlet.init(GenericServlet.java:212)
com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:80)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:379)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
小智 71
我们的测试看起来像这样(使用Maven和Spring 3.1):
@ContextConfiguration
(
{
"classpath:beans.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/dispatcher-data-servlet.xml",
"file:src/main/webapp/WEB-INF/spring/dispatcher-servlet.xml"
}
)
@RunWith(SpringJUnit4ClassRunner.class)
public class CCustomerCtrlTest
{
@Resource private ApplicationContext m_oApplicationContext;
@Autowired private RequestMappingHandlerAdapter m_oHandlerAdapter;
@Autowired private RequestMappingHandlerMapping m_oHandlerMapping;
private MockHttpServletRequest m_oRequest;
private MockHttpServletResponse m_oResp;
private CCustomerCtrl m_oCtrl;
// more code ....
}
Run Code Online (Sandbox Code Playgroud)
axt*_*avt 40
这就是不配置的原因webapp.
据我所知,没有很好的方法可以webapp从单元测试中访问文件夹中的文件.您可以src/main/resources改为使用配置,以便可以从单元测试(如文档中所述)以及webapp(使用classpath:前缀contextConfigLocation)访问它.
也可以看看:
fas*_*seg 20
我认为这是一个特定的maven问题.Maven不会将文件格式复制/src/main/resources到target-test文件夹.如果您绝对想要这样,那么您必须通过配置资源插件自己完成此操作.
更简单的方法是将测试特定的上下文定义放在/src/test/resources目录中并通过以下方式加载:
@ContextConfiguration(locations = { "classpath:mycontext.xml" })
Run Code Online (Sandbox Code Playgroud)
Bal*_*ala 11
简单的解决方案
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext.xml" })
Run Code Online (Sandbox Code Playgroud)
来自春季论坛
我们用:
@ContextConfiguration(locations="file:WebContent/WEB-INF/spitterMVC-servlet.xml")
Run Code Online (Sandbox Code Playgroud)
该项目是一个eclipse动态Web项目,然后路径是:
{project name}/WebContent/WEB-INF/spitterMVC-servlet.xml
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
183384 次 |
| 最近记录: |