Darko Romanov Darko Romanov - 2 months ago 21
Java Question

Error running Spring boot tests in IntelliJ Community 2017.1

This is a very weird behavior. I have a Spring boot application which works fine. Since a couple of days (I just switched the app to another Git repo, if it means something) every time I run tests with IntelliJ I gest the error:


java.lang.IllegalStateException: Failed to load ApplicationContext at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at
org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at
org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)
at
org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)
at
org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at
org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at
org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at
com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalStateException: Failed to add
PropertySource to Environment at
org.springframework.test.context.support.TestPropertySourceUtils.addPropertiesFilesToEnvironment(TestPropertySourceUtils.java:198)
at
org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:100)
at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 24 common frames omitted Caused by:
java.io.FileNotFoundException: class path resource
[com/mycompany/myproject/test.properties] cannot be opened because it
does not exist at
org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at
org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:154)
at
org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
at
org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:72)
at
org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:58)
at
org.springframework.core.io.support.ResourcePropertySource.(ResourcePropertySource.java:84)
at
org.springframework.test.context.support.TestPropertySourceUtils.addPropertiesFilesToEnvironment(TestPropertySourceUtils.java:194)
... 27 common frames omitted 09:54:42.723 [main] DEBUG
org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener
- After test class: context [DefaultTestContext@371a67ec testClass = AudioVideoControllerTest, testInstance = [null], testMethod = [null],
testException = [null], mergedContextConfiguration =
[WebMergedContextConfiguration@5ed828d testClass =
AudioVideoControllerTest, locations = '{}', classes = '{class
com.mycompany.myproject.myprojectApplication}',
contextInitializerClasses = '[]', activeProfiles = '{}',
propertySourceLocations =
'{classpath:/com/mycompany/myproject/test.properties}',
propertySourceProperties =
'{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}',
contextCustomizers =
set[org.springframework.boot.test.context.SpringBootTestContextCustomizer@548e7350,
org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@77cd7a0,
org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@754ba872,
org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0,
org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0,
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@13805618],
resourceBasePath = 'src/main/webapp', contextLoader =
'org.springframework.boot.test.context.SpringBootContextLoader',
parent = [null]]], class annotated with @DirtiesContext [false] with
mode [null]. Process finished with exit code -1


So it says it can't find the test.properties file I declare here:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyApplication.class)
@TestPropertySource(locations="test.properties")
@SpringBootTest
public class AudioVideoControllerTest {...}


The file is in the classpath (src/main/java/com/mycompany/myproject/test.properties)

Now the weirdest part. If I open the project with Eclipse (Sprig Tool Suite actually) and I run tests, they work. Then I run again tests on IntelliJ and... they work!

So it looks like Eclipse/STS makes some changes when I open and refresh the project but I wonder what it is.

Answer Source

Solution is dragging the test.properties file to another folder, let IntelliJ update references and then move again the test.properties file back into the root folder and, again, let IntelliJ update all references.