1

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 am 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.

Code Junkie
  • 7,602
  • 26
  • 79
  • 141
  • Possible duplicate of [How to test Classes with @ConfigurationProperties and @Autowired](https://stackoverflow.com/questions/31745168/how-to-test-classes-with-configurationproperties-and-autowired) – Lorelorelore Nov 06 '18 at 20:39
  • @Lorelorelore this is not a duplicate, I'm looking to dynamically set the value in the ApplicationConfig for each test, not set it once and read the same value for every test. – Code Junkie Nov 06 '18 at 20:45

1 Answers1

0

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.

Kirill Shiryaev
  • 339
  • 2
  • 8