10

我正在一个项目中从 junit4 过渡到 junit5,并试图弄清楚如何测试日志。以前,我用

@Rule
OutputCapture outputCapture = new OutputCapture();

然后会使用 写一个断言outputCapture.toString(),例如

assertThat(outputCapture.toString(),containsString("status=200"));

由于@Rulejunit5中还没有实现注解,所以不能使用outputCapture。有什么想法可以代替吗?谢谢!

4

4 回答 4

7

提供了一个扩展,您可以按如下方式使用它:

@ExtendWith(OutputCaptureExtension.class)
public class MyTestClass {

    @Test
    void myTestCase(CapturedOutput capturedOutput) {
        assertTrue(capturedOutput.getOut().contains("expected string"));
        assertTrue(capturedOutput.getErr().contains("expected string"));
    }
}
于 2021-04-12T09:00:07.737 回答
6

我们在 JUnit5 迁移过程中偶然发现了同样的问题。经过一番研究,我找到了一个技术解决方案,但似乎还没有人用它制作一个测试库。所以这就是我所做的。它已发布到 Maven Central,因此您可以立即使用它:

https://github.com/netmikey/logunit

您可以按如下方式使用它:

public class MyModuleTest {

    @RegisterExtension
    LogCapturer logs = LogCapturer.create().captureForType(MyModule.class);

    @Test
    public void testLogging() {
        // ... do the testing ...

        // Run assertions on the logged messages
        logs.assertContains("Some message");
    }
}

(有关更多示例,请参阅项目的自述文件)

于 2019-03-15T10:07:47.320 回答
3

JUnit5 文档中的迁移提示明确指出 -

@Rule并且@ClassRule不再存在;@ExtendWith被;取代 有关部分规则支持,请参见以下部分。

为了使用来自 JUnit 4的现有@Rule支持,有一种方法建议用于方法或类级别的注释。

与 JUnit 4 一样,支持带有规则注释的字段和方法。通过在测试类上使用这些类级扩展,遗留代码库中的此类规则实现可以保持不变,包括 JUnit 4 规则导入语句。

这种有限形式的规则支持可以通过类级别注释打开 org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport


如果您正在使用它,更好的选择仍然是重新设计您的测试套件以使用JUnit5 的扩展模型。

于 2017-03-13T17:35:33.323 回答
1

您还可以通过自行快速实施解决方案来轻松测试写入System.out的日志输出,如下所示:

// Configure System.out to be written to a buffer from which we can read
PrintStream realSysOut = System.out;
BufferedOutputStream sysOutBuffer = new ByteArrayOutputStream();
System.setOut(new PrintStream(sysOutBuffer));
...
// Perform some action which logs something to System.out
System.out.println("Some random content written to System.out");
...
// Assert that a given string was written in the meantime to System.out
assertThat(new String(buffer.toByteArray()), containsString("random content"));
...
// Don't forget to bring back the real System.out at the end of the test
System.setOut(realSysOut);

在检查写入 System.err 的日志输出的情况下,您可以通过将System.setOut(...)替换为System.setErr(...)并将System.out替换为System.err在上面的示例中等效地实现它。

于 2020-10-21T06:54:50.993 回答