Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

java - Springboot unit test set @Configuration Properties dynamically

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"));
    }
}

You have to use @Resource because @Autowired don't work in this case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...