【问题标题】:SpringBootTest annotation in test classes测试类中的 SpringBootTest 注解
【发布时间】:2019-09-15 07:28:32
【问题描述】:

在添加第二个测试类后,我在 jUnit 测试期间出现错误。

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

...所以我在 SpringBootTest 注释中添加了 (classes=...) 部分,并且测试运行良好。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {mySecondJUnitClass.class})
public class mySecondJUnitClass{

我不明白的是,我没有将“类”添加到我的第一个测试类中,我只设置了 @SpringBootTest 注释并且工作正常。

@RunWith(SpringRunner.class)
@SpringBootTest
public class myFirstJUnitClass{

我们何时以及为什么需要这个“类”定义?为什么@SpringBootTest 还不够?

【问题讨论】:

  • 您的测试可能不在主类包的子包中。发布你的项目布局:主类在哪里,测试在哪里。
  • 是的!就是这个。谢谢你。当我设置子包名称时,它在没有类定义的情况下运行!

标签: java spring-boot junit


【解决方案1】:

我找到了答案(在JB Nizet 的帮助下)。我的第一个测试类在我的主类包的子包中,第二个不在。所以这就是为什么它需要一个类定义。

【讨论】:

    猜你喜欢
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 2017-07-24
    • 2020-01-10
    • 2017-05-28
    • 2019-03-12
    相关资源
    最近更新 更多