使用SpringJUnit4ClassRunner的Junit TestSuite

时间:2016-08-27 12:10:06

标签: java spring junit spring-boot

我有一个测试套件:

@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
}

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

  1. 此设置有什么问题?
  2. properties文件的本地化吗?
  3. 我可以使用@TestPropertySource吗?
  4. EDIT 造成的是:

      

    引起:java.io.FileNotFoundException:类路径资源   无法打开[testApplication.properties],因为它没有   存在

    但是为什么找不到test/java/testApplication.properties中的位置?

1 个答案:

答案 0 :(得分:2)

尝试解决问题后,问题似乎properties文件必须位于test/java/resources/testApplication.properties。所以答案是将属性文件放入resources目录。

相关问题