Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...