【发布时间】: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