"Could not open ServletContext resource" in test context

Florian :

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/

Moshe Arad :

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Integration Test - Could not Open ServletContext Resource /application/properties

FileNotFoundException : Could not open ServletContext resource [/WEB-INF/spring/root-context.xml]

Could not open ServletContext resource

Could not open ServletContext resource -- except there is no reference to the resource

Error Could not open ServletContext resource in jUnit Testing Spring Integration

Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/applicationContext.xml]

spring boot FileNotFoundException: Could not open ServletContext resource [/WEB-INF/main-servlet.xml]

Could not open ServletContext resource [/<NONE>] when upgrade spring-boot-actuator 1.2.5.RELEASE

Spring Boot exception: Could not open ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]

java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]

Spring+ MyBatis+ mvn Project , java.io.FileNotFoundException: Could not open ServletContext resource [/classpath*:WEB-INF/myBatis-config.xml]

Context Configuration in spock test

Spring boot test: context loaded for every test?

Failed to retrieve PlatformTransactionManager for @Transactional test for test context

How to load beans into test context?

@Context is null during integration test

library test: how to create a Context?

Spring test : Missing servlet context

@Value is not injecting in SpringBoot Test Context

Can't load context for Test

How to test "this" calling context with jest?

How to use the LocalizationUtility in test context

Populate context in unit test for webclient

Test method mockito with spring context

Unit test context configuration spring

Unit test http context in Angular

aws lambda test with context identity