I'm trying to create a Unit Test with Spring.
Test class:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyConfig.class})
public class MyTest{
@Test
public void ...
}
Class to load:
@ConfigurationProperties()
@PropertySource("config/myConfig.properties")
@Component
public class MyConfig {}
Exception:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [de.db.sus.converter.fia.business.algorithm.config.FiaConverterConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/config/myConfig.properties]
I have found resources for web applications and/or xml based configurations, but wasn't apply to transfer them.
If I would start the application with @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
the properties get loaded. But I can't start the whole application for every Unit test.
I have verified that the file exists in the directory test/resources/config/
It seems that the requested properties can't be found. I would recommend doing this:
If the requested properties file is within your classpath you can fix above with just writing the next line:
@PropertySource("classpath:config/myConfig.properties")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With