Spring override non-application.properties file in Tests

Viewed 32

I use an external pure Java library that uses a file that is called updater.properties. Im currently writing some Spring-Boot Integration Tests with @SpringBootTest. The problem I currently have is that i don’t find any option to pass/use a different updater.properties file with Spring. Is there a solution for this? Or does the library need to be updated for this to work? The updater.properties is in my resource folder and being parsed internally by the library. Would be happy for any help!

1 Answers

@SaltukKezer, yes you can. First step make a config class like

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:/updater.properties")
public class UpdaterProperties{


     @Autowired
     Environment env;

     public String getMyPropertyValue() {
        return env.getProperty("your.property.name");
     }
  
 }

Then second step, just get an instance of this class and call the getters

Related