How to access a file from resources folder in test in springboot

593 Views Asked by At

My file "products.yml" is located in src/test/resources/configs folder. I have defined config import in my application.yml in springboot

spring:
  config:
    import:
      - optional:configtree:src/test/resources/configs/

I am trying to get this "products.yml" filepath using @Value annotation in my configuration class

@Configuration
public class ProductConverterConfiguration {

@Value("${products}") 
private String productsYamlFile;

System.out.Println("Got the file as: " + productsYamlFile);
}

Springboot is unable to see that file when I try to run bazel test for that class it fails with null pointer exception.

I tried giving absolute path in the application.yaml and it works. But I need the relative path since I wont have the same host when I deploy this application. Can someone help me with this?

1

There are 1 best solutions below

0
On

Can you try this code ?

application.yml

spring:
  config:
    import:
      - optional:configtree:classpath:configs/

ProductConverterConfiguration class

@Configuration
public class ProductConverterConfiguration {

    @Value("${products}") 
    private String productsYamlFile;

    @PostConstruct
    public void init() {
        System.out.println("Got the file as: " + productsYamlFile);
    }
}

(can you chare product.yml if it doesn't work)