Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@TestPropertySource not working?

I am trying to test my Spring Configuration. Here is my config class

@Configuration
@PropertySource("/etc/my.properties")
@ComponentScan("my.package")
public class SpringConfig {

    @Bean
    .....
}

And when I tried to test it thru my test as

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(locations = "classpath:test.properties")
@ContextConfiguration(classes = {SpringConfigIntTest.class, SpringConfig.class})
public class SpringConfigIntTest {
    .......
}

I keep getting the failure saying /etc/my.properties cannot be found. But I think I have overridden it with test.properties and I have been googling around without seeing other people doing it differently.

I am using spring-context, spring-beans, spring-core 4.2.2, and spring-test 4.2.4. Can someone please give me some help here? Deeply appreciate it

like image 861
gigi2 Avatar asked May 13 '26 17:05

gigi2


1 Answers

You can set the property source to not fail if the resource does not exist:

@PropertySource(value = "/etc/my.properties", ignoreResourceNotFound = true)
like image 90
David Siro Avatar answered May 15 '26 12:05

David Siro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!