2012-09-26 182 views
1

我想配置JUnit与Spring一起工作,但我不能。JUnit和弹簧配置

我创建了一个测试类是这样的:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:/web-application-config.xml"}) 
public class MyControllerToTest { 
     @Autowired 
    private MyController ctr; 

    @Test 
    .... 
} 

在classpath中,我插入的src/main /资源,其中,web应用-config.xml中的位置。下面的内容:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

<!-- Scans for application @Components to deploy --> 
<context:component-scan base-package="com.infoone.mycontrollerpackage"/> 

<!-- Imports the configurations of the different infrastructure systems of the application --> 
<import resource="webflow-config.xml" /> 
<import resource="webmvc-config.xml" /> 
<import resource="data-access-config.xml" /> 
<import resource="repository-config.xml" /> 
<import resource="security-config.xml" /> 

当我将其作为JUnit运行MyControllerToTest类,我得到如下:

ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener 
[org.springframewor[email protected]6419fa] to prepare test instance [[email protected]] 
java.lang.IllegalStateException: Failed to load ApplicationContext 
... 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowExecutor': Cannot resolve reference to bean 'flowRegistry' while setting bean property 'flowDefinitionLocator'; nested exception is   org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowRegistry': Invocation of init method failed; nested exception is java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml' 
... 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowRegistry': Invocation of init method failed; nested exception is java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml' 
... 
Caused by: java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml' 
... 
Caused by: java.io.FileNotFoundException: class path resource [WEB-INF/flows/] cannot be resolved to URL because it does not exist[/code] 

谢谢!

回答

3

问题是WEB-IN F文件夹在测试时(使用maven)不太容易,因为它在测试时位于其他位置。

我的意思是:

  • 当你运行测试,你在文件夹中运行编译的类:<project>/target/test-classesWEB-INF文件夹位于<project>/target
  • 当您运行的WEB-INF位于应用程序<war>/WEB-INF而你的班<war>/WEB-INF/classes

我想说什么抵抗,在WEB-INF文件夹到资源的相对路径是二测试和应用之间的差异。

无论如何,有一个名为spring-test-mvc的春季项目,也许你可以使用它或使用你在代码中找到的一些想法。

+0

谢谢,非常有帮助。你会建议将流量从WEB-INF中移出吗?我可以将它们移动到src/main/resources,但是,在这种情况下,我不确定如何从我的网页中引用它们... – Manu

+1

@Manu:将src/main/resources的内容合并到构建应用程序时应用程序的类路径根。 – Ralph

0

您需要在@ContextConfiguration内添加file:src并添加@WebAppConfiguration注释。所以测试类应该看起来像这样:

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ContextConfiguration(locations = {"file:src:/path.../web-application-config.xml"}) 
public class MyControllerToTest { 
     @Autowired 
    private MyController ctr; 

    @Test 
    .... 
}