Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not open ServletContext resource" in test context

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/

like image 962
Florian Avatar asked Sep 29 '16 12:09

Florian


1 Answers

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")

like image 185
Moshe Arad Avatar answered Oct 29 '22 04:10

Moshe Arad