Reputation: 2921
I have a test suite:
@RunWith(Suite.class)
@Suite.SuiteClasses({
A.class,
B.class
})
public class TestSuite {
}
A class:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class A {
//nothing
}
B class:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@TestPropertySource(value = "classpath:testApplication.properties")
public class B {
//nothing
}
Now the problem is with @TestPropertySource
the testApplication.properties
is located under the test/java/testApplication.properties
and the A
and B
classes are under test/java/com.example.MyApp
.
If I comment the line with the @TestPropertySource
test suit runs ok but with it uncommented I get:
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.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener.prepareTestInstance(AutoConfigureReportTestExecutionListener.java:49) 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)
properties
file?EDIT The caused by is:
Caused by: java.io.FileNotFoundException: class path resource [testApplication.properties] cannot be opened because it does not exist
However why the location in test/java/testApplication.properties
is not being found?
Upvotes: 1
Views: 2183
Reputation: 2921
After trying to solve the issue the problem seem that the properties
file must be in test/java/resources/testApplication.properties
. So the answer is put the properties file into resources
directory.
Upvotes: 2