12

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/

3 Answers 3

24

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this helps. In short: "classpath:" is missing
0

I configured maven war plugin as below

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
       <warName>${war-file-name}</warName>
        <warSourceDirectory>src/main/webapp</warSourceDirectory>
        <failOnMissingWebXml>false</failOnMissingWebXml>

        <webResources>
          <resource>
            <!-- this is relative to the pom.xml directory -->
            <directory>src/main/resources</directory>
          </resource>
        </webResources>
     </configuration>
</plugin>

Configure the following section as relevant to your pom.xml

 <webResources>
          <resource>
            <!-- this is relative to the pom.xml directory -->
            <directory>src/main/resources</directory>
          </resource>
 </webResources>

For more information refer Adding and Filtering External Web Resources

Comments

0

I had same error but I had to define my resources file reference with classpath as I was loading file from properties file

spring.rabbitmq.ssl.key-store=classpath:/client-file

You can also load from filesystem - see here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.