繁体   English   中英

在运行测试时从项目根目录而不是模块加载Spring Boot文件系统资源/ config

[英]Load Spring Boot file system resource / config from project root directory instead of module's while running tests

我有一个Spring Boot多模块项目,其根目录中有一个配置文件:

project-root
|
\-- config
|   \-- file.yaml
|
\-- module1
|   \-- pom.xml
|
\-- module2
|   \-- src
|   |   \-- main
|   |   |   \-- ...
|   |   \-- test
|   |       \-- java
|   |           \-- test.java
|   |
|   \-- pom.xml
|
\-- module3
|   \-- pom.xml
...
|
\-- moduleN
|   \-- pom.xml
|
\-- pom.xml

模块中的某些类(例如project-root/module2/src/main/**.java )使用以下file.yaml加载file.yaml

new FileSystemResource("config/file.yaml");

在他们的构造函数中成功,但是当我运行test.java ,它包含:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Test.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class Test {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void test() {
        String body = this.restTemplate.getForObject("/", String.class);
        assertThat(body)...
        ...
    }

    ...
}

并且覆盖了资源加载代码,我得到了FileNotFoundExceptionfile.yaml应该在project-root/module2/config (而不是project-root/config )。 为什么?

您尚未说明如何运行测试,但是使用Maven运行测试时,它将工作目录更改为正在构建和测试的模块目录。 在Maven的术语中,这被称为basedir这是记录在这里 如果您在IDE中运行测试,则可能会模仿Maven的行为以保持一致性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM