[英]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: B班:
@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
. 现在问题出在
@TestPropertySource
, testApplication.properties
位于test/java/testApplication.properties
,而A
和B
类位于test/java/com.example.MyApp
。 If I comment the line with the @TestPropertySource
test suit runs ok but with it uncommented I get: 如果我用
@TestPropertySource
测试服注释该行运行正常,但没有注释,我得到:
java.lang.IllegalStateException: Failed to load ApplicationContext
java.lang.IllegalStateException:无法加载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
在org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)在org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)在org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)在org.springframework.boot.test org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)上的.autoconfigure.AutoConfigureReportTestExecutionListener.prepareTestInstance(AutoConfigureReportTestExecutionListener.java:49)在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.Class.Spring :228)在org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)在org.springframework在org.springframework.test.context.junit4.SpringJ的.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) Unit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)的Unit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
properties
file? properties
文件的本地化吗? EDIT The caused by is: 编辑的原因是:
Caused by: java.io.FileNotFoundException: class path resource [testApplication.properties] cannot be opened because it does not exist
原因:java.io.FileNotFoundException:类路径资源[testApplication.properties]无法打开,因为它不存在
However why the location in test/java/testApplication.properties
is not being found? 但是,为什么
test/java/testApplication.properties
的位置?
After trying to solve the issue the problem seem that the properties
file must be in test/java/resources/testApplication.properties
. 尝试解决此问题后,似乎该
properties
文件必须位于test/java/resources/testApplication.properties
。 So the answer is put the properties file into resources
directory. 因此,答案是将属性文件放入
resources
目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.