Java Springboot集成测试:在bean初始化之前用pwd更新application.yml属性
我正在编写Springboot集成测试。src/test/resources中有一个application.yml。 yml中有一个属性:Java Springboot集成测试:在bean初始化之前用pwd更新application.yml属性,java,spring,spring-boot,Java,Spring,Spring Boot,我正在编写Springboot集成测试。src/test/resources中有一个application.yml。 yml中有一个属性: testFilePath:“projectBaseDirectoryPathVariable/src/test/resources/testFiles” 测试中使用的bean必须在bean(类)构造函数中读取该值“testFilePath” 在执行集成测试之前,我希望能够将projectBaseDirectoryPathVariable的值替换为用户目录(或
testFilePath:“projectBaseDirectoryPathVariable/src/test/resources/testFiles”
测试中使用的bean必须在bean(类)构造函数中读取该值“testFilePath”
在执行集成测试之前,我希望能够将projectBaseDirectoryPathVariable
的值替换为用户目录(或PWD路径)。因此,bean初始化具有正确的完整路径,即/users/usrname/projects/project\u name/src/test/resources/testFiles
我尝试使用BeforeAll()方法,该方法将替换application.yml文件内容,从而将projectBaseDirectoryPathVariable替换为完整路径。但是bean(类)仅在调用其构造函数时使用旧值“projectBaseDirectoryPathVariable”
处理这个案子的方法是什么
这就是测试:
@Slf4j
@ActiveProfiles({"test", "master"})
@SpringBootTest
public class MyIntegrationTest {
@Autowired
ClassUsingApplicationYml classUsingApplicationYml;
@BeforeAll
static void beforeAll() throws IOException {
//Some code
}
@Test
void dummyClassTest() {
// Some code
}
}
使用applicationyml/bean进行分类
@Slf4j
@Component
public class ClassUsingApplicationYml implements Tasklet {
public ClassUsingApplicationYml(@Value("${file.results}") String resultsDirectory) {
//resultsDirectory should get "/users/usrname/projects/project_name/src/test/resources/testFiles",
//but it is getting "projectBaseDirectoryPathVariable/src/test/resources/testFiles"
this.resultsDirectory = resultsDirectory;
}
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws IOException {
//Some code
log.info(resultsDirectory);
return RepeatStatus.FINISHED;
}
}
application.yml
file
results: "projectBaseDirectoryPathVariable/src/test/resources/testFiles"
你可以稍微修改一下
file
property : projectBaseDirectoryPathVariable
results: ${file.property}/src/test/resources/testFiles
现在,使用要覆盖它的环境变量
FILE\u属性
开始测试。这可以通过System.setEnv(…)
在@BeforeAll
中完成,并通过System.clearProperty
在@AfterAll
中清除。如何在集成测试中从application.yml访问file.property的值?我无法在集成测试中访问file.property。有人能帮忙吗?