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
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.
Example
@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.
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…