简体   繁体   中英

Junit TestSuite with SpringJUnit4ClassRunner

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.SpringJ Unit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)

  1. What is wrong with this setup?
  2. Is it the localization of the properties file?
  3. Shall I maybe use @TestPropertySource ?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM