0

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?

Dattebayo
  • 3
  • 2
  • Does this answer your question? [Spring boot: How to read resource from classpath in unit test](https://stackoverflow.com/questions/43428305/spring-boot-how-to-read-resource-from-classpath-in-unit-test) – ℛɑƒæĿᴿᴹᴿ Jul 20 '23 at 16:57
  • When you deploy the application, you won't have a `src` folder either. So `src/test/resources/configs` is definitely wrong there. – k314159 Jul 20 '23 at 17:00
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jul 20 '23 at 20:25

1 Answers1

0

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)

Enes Birisik
  • 101
  • 4