【问题标题】:Junit TestSuite with SpringJUnit4ClassRunner带有 SpringJUnit4ClassRunner 的 Junit 测试套件
【发布时间】: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 下,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) 在 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 吗?

编辑 原因是:

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

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

【问题讨论】:

  • 如果你使用intellij insted of eclipse你会立即看到问题

标签: java spring junit spring-boot


【解决方案1】:

在尝试解决问题后,问题似乎是properties 文件必须在test/java/resources/testApplication.properties 中。所以答案是把properties文件放到resources目录下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 2022-08-20
    • 2015-07-21
    • 2012-10-12
    • 1970-01-01
    相关资源
    最近更新 更多