【问题标题】:Can't find properties file when running gradle test运行gradle测试时找不到属性文件
【发布时间】:2014-03-13 12:31:58
【问题描述】:

我正在使用 gradle 来构建和测试我的应用程序。我使用命令

gradle test

但是在 Eclipse 中运行时可以完美运行的测试在使用 gradle 运行时会失败。我的测试在下面定义,它找不到的属性文件在这两个文件夹中:

/home/user/Development/git/myproject/src/main/java/com/mycompany/config /home/user/Development/git/myproject/src/test/resources

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {      ApplicationConfig.class })
public class WebImportTest {
    @Resource
    private Environment env;

    @Autowired
    private JestClient jestClient;

我的 java 配置如下所示:

@Configuration
@ComponentScan({ "com.mycompany" })
@PropertySource("classpath:/com/mycompany/config/myproject.properties")
@Import(PersistenceConfig.class)
public class ApplicationConfig {

    @Resource
    private Environment env;

我没有足够的经验来弄清楚这是什么原因

:myproject is getting tests from [task ':test']
:compileJava
Note: /home/user/Development/git/myproject/src/main/java/com/mycompany/entities/generated/Keys.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:processResources UP-TO-DATE
:classes
:instrument SKIPPED
:copyCoberturaDatafile SKIPPED
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test

com.mycompany.myProject.DBConnectionTest > initializationError FAILED
java.lang.Exception

com.mycompany.myProject.WebImportTest > testWebImport FAILED
    java.lang.IllegalStateException
        Caused by: org.springframework.beans.factory.BeanDefinitionStoreException
            Caused by: java.io.FileNotFoundException

为什么找不到属性文件?!

【问题讨论】:

    标签: java junit gradle


    【解决方案1】:

    属性文件需要进入src/main/resources/com/mycompany/config

    【讨论】:

    • 就是这样。这意味着如果在测试期间使用了 java 配置文件,它将尝试从相应的资源文件夹中获取属性文件。好的。谢谢
    • 等一下。如果属性文件仅用于测试,它应该放在src/test/resources/com/mycompany/config 下。如果它应该与生产代码一起提供,它应该在src/main/resources/com/mycompany/config 下。
    • 是否无法将 build.gradle 配置为在项目根目录的 config 目录等自定义位置具有属性文件?
    猜你喜欢
    • 2021-07-09
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 2018-08-17
    相关资源
    最近更新 更多