【发布时间】:2016-08-27 12:10:05
【问题描述】:
我有一个测试套件:
@RunWith(Suite.class)
@Suite.SuiteClasses({
A.class,
B.class
})
public class TestSuite {
}
一个类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class A {
//nothing
}
B类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@TestPropertySource(value = "classpath:testApplication.properties")
public class B {
//nothing
}
现在问题在于@TestPropertySource testApplication.properties 位于 test/java/testApplication.properties 下,A 和 B 类位于 test/java/com.example.MyApp 下。
如果我用@TestPropertySource 测试套装运行正常但未注释我得到:
java.lang.IllegalStateException: 无法加载 ApplicationContext
在 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) 在 org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) 在 org.springframework.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener.prepareTestInstance(AutoConfigureReportTestExecutionListener.java:49) 在 org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
- 此设置有什么问题?
- 是
properties文件的本地化吗? - 我可以使用@TestPropertySource 吗?
编辑 原因是:
原因:java.io.FileNotFoundException:类路径资源 [testApplication.properties] 无法打开,因为它没有 存在
但是为什么找不到test/java/testApplication.properties 中的位置?
【问题讨论】:
-
如果你使用intellij insted of eclipse你会立即看到问题
标签: java spring junit spring-boot