问题描述
我有一个在命令行上运行的 springboot 2 应用程序.命令行参数之一是 fileName ,命名中带有 batchNo .我正在使用命令行参数中的 fileName 设置我的 application.properties fileName 值.示例 batch-1234.csv
I have a springboot 2 application that runs on the commandline. One of the commandline args is fileName with a batchNo in the naming. I'm setting my application.properties fileName value with the fileName from the commandline arg. Example batch-1234.csv
在我的 ApplicationConfig 文件中,我像这样从 Applications.properties 文件中读取文件名.
In my ApplicationConfig file I'm reading the filename from the applications.properties file like so.
@ToString
@Setter
@Getter
@Configuration
@ConfigurationProperties(prefix = "hri")
public class ApplicationConfig {
@Value("${company.file}")
private String file;
public String getBatchNo() {
return parseBatchNo(file);
}
public String getHomecareDetailPath() {
return filePathProvider.getFilePath("batch-" + getBatchNo() + ".csv");
}
private static String parseBatchNo(String file) {
if (batchNo == null) {
batchNo = file.substring((file.lastIndexOf("-") + 1), (file.length() - 4));
}
return batchNo;
}
}
我的目标是能够为每个单独的测试动态设置这个文件名.
My goal is to be able to set this file name for each individual test dynamically.
例子
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdapLoadApplication.class)
@AutoConfigureTestDatabase
public class CorporateServiceTest {
@Autowired
private ApplicationConfig config;
@Test
public void convertCsvToBean() throws IOException {
//Set file
config.setFile("batch-9999.csv);
}
@Test
//Is there an annotation to set the Application.properties file for each test?
public void test2() throws IOException {
//Set file
config.setFile("batch-8888.csv);
}
}
如何动态设置文件以便更好地管理测试?目前我们正在使用相同的文件,并且必须进行大量文件复制以覆盖测试文件.
How could I dynamically set the file so that I can better manage my test? Currently we are using the same file and having to do a lot of file copying overwriting a test file.
我想澄清一下,我不想在我的 Test 类中创建一个新的 ApplicationConfig bean 并设置文件并从每个单独的测试中读取相同的文件.我想在每个单独的测试开始时设置该文件名.
I would just like to clarify, I'm not looking to just create a new ApplicationConfig bean in my Test class and set the file and read the same file from each individual test. I would like to set that file name in the beginning of each individual test.
推荐答案
例如你可以使用 Environment:
For example you can use Environment:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdapLoadApplication.class)
@AutoConfigureTestDatabase
public class CorporateServiceTest {
@Autowired
private ApplicationConfig config;
@Resource
private Environment env;
@Test
public void convertCsvToBean() throws IOException {
//Set file
config.setFile(env.getProperty("first_test_company.file"));
}
@Test
//Is there an annotation to set the Application.properties file for each test?
public void test2() throws IOException {
//Set file
config.setFile(env.getProperty("second_test_company.file"));
}
}
你必须使用@Resource,因为@Autowired 在这种情况下不起作用.
You have to use @Resource because @Autowired don't work in this case.
这篇关于Springboot 单元测试集动态设置@Configuration 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!