首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpingBoot OutputCaptureRule在扔IllegalStateException

SpingBoot OutputCaptureRule在扔IllegalStateException
EN

Stack Overflow用户
提问于 2021-12-26 11:26:09
回答 1查看 72关注 0票数 0

我正在按照https://www.docs4dev.com/javadoc/en/org/springframework/boot/spring-boot-test/2.2.2.RELEASE/org/springframework/boot/test/system/OutputCaptureRule.html中的指示行事

我正在使用Maven与spring启动器-父版本2.2.2。

我的测试很简单:

代码语言:javascript
复制
@SpringBootTest(classes = MyApplication.class) // this loads Springboot context
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@ContextConfiguration(classes = {MyTestConfig.class, AnotherTestConfig.class})
public class MyTest {


    @Rule
    public OutputCaptureRule output = new OutputCaptureRule();

    @Test
    public void theTest() {
        assertThat(output).contains("something");
    }

}

但是,当我在assert行中放置一个断点并试图计算output.getOut()时,结果是:方法抛出了'java.lang.IllegalStateException‘异常。详细信息是:没有找到系统捕获。请检查您的输出捕获注册。

这个功能似乎无法发挥作用。知道我错过了什么吗?

EN

回答 1

Stack Overflow用户

发布于 2021-12-26 11:46:09

这很可能是因为您使用的是JUnit 5,因为SpringBoot 2.2默认提供JUnit 5,但是OutputCaptureRule是JUnit 4的TestRule组件,因此不能在JUnit 5下激活。

您应该在OutputCaptureExtension 5中使用等效的JUnit 5:

代码语言:javascript
复制
@SpringBootTest(classes = MyApplication.class) 
@ExtendWith(OutputCaptureExtension.class)
public class MyTest {

    @Test
    public void theTest(CapturedOutput output) {
        assertThat(output).contains("something");
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70486065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档