2010-12-07 30 views
43

在我們的項目,我們書面方式測試,以檢查是否控制器返回正確的模型視圖春@ContextConfiguration如何把正確的位置爲XML

@Test 
    public void controllerReturnsModelToOverzichtpage() 
    { 
     ModelAndView modelView = new ModelAndView(); 
     KlasoverzichtController controller = new KlasoverzichtController(); 
     modelView = controller.showOverzicht(); 

     assertEquals("Klasoverzichtcontroller returns the wrong view ", modelView.getViewName(), "overzicht"); 
    } 

這返回異常空。

我們現在配置@ContextConfiguration,但我們不知道如何加載誰是位於SRC \主\ web應用程序\ ROOT \ WEB-INF \根context.xml中對xml

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
public class TestOverzichtSenario{ 
.... 

This documentation isn't clear enough to understand

任何有關如何確保contextannotation加載正確的XML的建議?

編輯V2

我複製的配置從WEBINF文件夾.xml文件來

src\main\resources\be\..a bunch of folders..\configuration\*.xml 

,改變在WEB-INF web.xml中

<param-name>contextConfigLocation</param-name> 
<param-value> 
      classpath*:configuration/root-context.xml 
      classpath*:configuration/applicationContext-security.xml 
     </param-value> 

和現在得到錯誤

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) 

回答

37

這就是不將配置置入webapp的原因。

據我所知,沒有好的方法來訪問單元測試中webapp文件夾中的文件。您可以將配置置入src/main/resources,以便您可以從單元測試(如文檔中所述)以及從webapp(使用contextConfigLocation中的classpath:前綴)訪問它。

參見:

+1

你有任何文檔引用什麼「classpath:」嗎? – David 2010-12-07 17:51:27

+0

@Dvd Prd:更新 – axtavt 2010-12-07 18:20:21

+0

thx,你可以檢查我的變化嗎? – David 2010-12-07 18:59:33

19

這是一個Maven具體的問題,我認爲。 Maven不會將文件/src/main/resources複製到目標測試文件夾。如果您絕對想要這樣做,您必須通過配置資源插件自己完成此操作。

更簡單的方法是不是把一個測試特定的上下文定義在/src/test/resources目錄和負載通過:

@ContextConfiguration(locations = { "classpath:mycontext.xml" }) 
5

我們使用:

@ContextConfiguration(locations="file:WebContent/WEB-INF/spitterMVC-servlet.xml") 

該項目是一個Eclipse的動態Web項目,那麼路徑是:

{project name}/WebContent/WEB-INF/spitterMVC-servlet.xml 
66

我們的測試看起來像這樣(使用M aven和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 .... 
} 
10

簡單的解決方案是

@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext.xml" }) 

spring forum

4

假設你要創建一個測試context.xml中這是獨立於應用程序-context.xml中進行測試,將test-context.xml放在/ src/test/resources下。在測試類中,在類定義的頂部有@ContextConfiguration註解。

@ContextConfiguration(locations = "/test-context.xml") 
public class MyTests { 
    ... 
} 

彈簧文件Context management

0

@RunWith(SpringJUnit4ClassRunner.class)來@ContextConfiguration(位置= { 「/Beans.xml」}) 公共類DemoTest {}

2

從加載文件: {project}/src/main/webapp/WEB-INF/spring-dispatcher-servlet.xml

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring-dispatcher-servlet.xml" })  
@WebAppConfiguration 
public class TestClass { 
    @Test 
    public void test() { 
     // test definition here..   
    } 
} 
-1

有時候它可能是很簡單的像你缺少資源文件在測試classses文件夾由於ŧ o一些清理。