Spring xml + JUnit - xml上下文文件在src / test / resources中不起作用但在src / main / resources下工作

时间:2014-07-29 20:47:05

标签: java xml spring maven junit

我遇到像这样的junit测试问题。出于某种原因,弹簧上下文文件只有在我将它们放在maven项目的src / main / resources文件夹中时才有效。 Intellij没有给我任何警告,但是mvn的junit测试跑者和ide炸弹。

以下两种情况下的代码和输出:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/test-applicationContext.xml"})
public class DataSharingTest {

    AuthTokenService authTokenService;

    @Autowired
    public void setAuthTokenService(AuthTokenService authTokenService) {
        this.authTokenService = authTokenService;
    }

    @Test
    public void MyTest(){
        System.out.println(authTokenService);
    }
}

如果文件位于src / test / resources / test-applicationContext.xml 我得到了

2014-07-29 16:44:18,061 main      ERROR TestContextManager    Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@69e328e0] to prepare test instance [com.jg.datasharing.DataSharingTest@4090c06f]
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [test-applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [test-applicationContext.xml] cannot be opened because it does not exist. If I try a wildcard in the path it will stop complaining about file not found but will compalain that the bean is missing.

如果我将文件移动到src / main / resources / test-applicationContext.xml,则传递

2014-07-29 16:46:53,564 main      INFO  lBeanDefinitionReader Loading XML bean definitions from class path resource [test-applicationContext.xml]
2014-07-29 16:46:53,778 main      INFO  ricApplicationContext Refreshing org.springframework.context.support.GenericApplicationContext@7426dbec: startup date [Tue Jul 29 16:46:53 EDT 2014]; root of context hierarchy
2014-07-29 16:46:53,904 main      INFO  ltListableBeanFactory Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@19202d69: defining beans [authTokenService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
com.jg.security.token.auth.JwtAuthTokenService@1380cf2a
2014-07-29 16:46:53,995 Thread-1  INFO  ricApplicationContext Closing org.springframework.context.support.GenericApplicationContext@7426dbec: startup date [Tue Jul 29 16:46:53 EDT 2014]; root of context hierarchy
2014-07-29 16:46:53,996 Thread-1  INFO  ltListableBeanFactory Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@19202d69: defining beans [authTokenService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

Process finished with exit code 0

为什么在测试上下文类路径中找不到文件?

2 个答案:

答案 0 :(得分:3)

检查测试/资源是否作为类路径添加。您可以在eclipse IDE中验证如下。

右键单击项目 - > properties-> java build path - >在Sources选项卡中,您应该找到{proejct} / src / test / resources(包括all的所有选项。)

如果找不到该选项,可以手动添加目录,也可以按如下方式配置pom.xml

<build>
   <testResources>
            <testResource>
                <directory>${basedir}/src/test/resources</directory>
            </testResource>
   </testResources>
</build>

答案 1 :(得分:1)

尝试使用

@ContextConfiguration("classpath*:test-applicationContext.xml")