-1

I have a configuration class as

@Configuration
@Getter
@Setter
@PropertySource(value = { "file:/config/appconfig.properties" })
public class ExternalProperties {
// varibles which get initize with @Value
}

which reads value from a file. But When I am trying to write integration test cases so at the time of initalization its failed as the appconfig.properties in present in the test env. And I am getting below error

Caused by: java.io.FileNotFoundException: /config/appconfig.properties (No such file or directory) at java.base/java.io.FileInputStream.open0(Native Method)

How to initizae the test enviroment in this case. what is the way we can point the PropertySouce to some other temp location?

Jose Manuel de Frutos
  • 1,040
  • 1
  • 7
  • 19
dead programmer
  • 4,223
  • 9
  • 46
  • 77

1 Answers1

0

You can first configure @PropertySource 's ignoreResourceNotFound as true such that the test can still continue executing in case of failure to load the properties file :

@PropertySource(value = { "file:/config/appconfig.properties" }, ignoreResourceNotFound = true)
public class ExternalProperties {

}

Then use @TestPropertySource to configure a properties file just for that test class such as :

@ExtendWith(SpringExtension.class)
@TestPropertySource("classpath:test.properties")
public class FooTest {

}
Ken Chan
  • 84,777
  • 26
  • 143
  • 172