简体   繁体   English

使用SpringJUnit4ClassRunner的Junit TestSuite

[英]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 . 现在问题出在@TestPropertySourcetestApplication.properties位于test/java/testApplication.properties ,而AB类位于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)

  1. What is wrong with this setup? 此设置有什么问题?
  2. Is it the localization of the properties file? 它是properties文件的本地化吗?
  3. Shall I maybe use @TestPropertySource ? 我应该使用@TestPropertySource吗?

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.

相关问题 如何使用Parametrized运行JUnit SpringJUnit4ClassRunner? - How to run JUnit SpringJUnit4ClassRunner with Parametrized? JUnit Test无法使用SpringJUnit4ClassRunner回滚 - JUnit Test not rollbacking with SpringJUnit4ClassRunner 将junit侦听器添加到SpringJUnit4ClassRunner - Add a junit listener to a SpringJUnit4ClassRunner 如何使用 SpringJUnit4ClassRunner 将映射器(mapstruct)注入 Junit 测试 - How to inject mapper (mapstruct) to Junit test with SpringJUnit4ClassRunner 在SpringJUnit4ClassRunner中使用spring @Conditional - spring @Conditional in SpringJUnit4ClassRunner 使用SpringJUnit4ClassRunner自定义BeanFactory? - Customize BeanFactory with SpringJUnit4ClassRunner? 找不到 SpringJUnit4ClassRunner 类 - SpringJUnit4ClassRunner class not found @RequestMapping与不同类中相同URL的“params”导致JUnit中的“IllegalStateException:无法映射处理程序”与SpringJUnit4ClassRunner - @RequestMapping with “params” on same URL in different classes cause “IllegalStateException: Cannot map handler” in JUnit with SpringJUnit4ClassRunner SpringJUnit4ClassRunner在JUnit测试用例结束时不关闭Application Context - SpringJUnit4ClassRunner does not close the Application Context at the end of JUnit test case SpringJUnit4ClassRunner和SpringRunner有什么区别 - What is the difference between SpringJUnit4ClassRunner and SpringRunner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM