org.junit.contrib.java.lang.system.StandardOutputStreamLog 在哪里?

Where is the org.junit.contrib.java.lang.system.StandardOutputStreamLog?

这个class在哪里? 我的项目使用 JUnit 4.12,并没有找到 StandardOutputStreamLog。 是否会在此版本中删除? 或者它来自其他库?这些丝束之间有什么关系?

此 class 已弃用,您应该使用 SystemOutRule

勾选source

StandardOutputStreamLog()

Deprecated.

Please use new SystemOutRule().enableLog().

Creates a rule that records writes while they are still written to the standard output stream.

我也有这个问题,事实是库 JUnit 4.12 没有包 org.junit.contrib.java.lang.system。您需要的库是系统规则。要解决此问题,您可以搜索 google "system-rules Junit",然后找到下载库的链接。例如现在可以在 link

下载

我希望这对某人有所帮助。

SystemOutRule 根据@Rahul Tripathi 的建议取代了 StandardOutputStreamLog

只需再添加一些提示即可立即开始。

Maven 依赖:

<dependency>
    <groupId>com.github.stefanbirkner</groupId>
    <artifactId>system-rules</artifactId>
    <version>1.18.0</version>
    <scope>test</scope>
</dependency>

将 jar 添加到构建路径以获取以下导入。

import org.junit.contrib.java.lang.system.SystemOutRule;

public void MyTest {
    @Rule
    public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();

    @Test
    public void writesTextToSystemOut() {
        System.out.print("hello world");
        assertEquals("hello world", systemOutRule.getLog());
    }
}

参考:http://stefanbirkner.github.io/system-rules/

StandardOutputStreamLog() 未弃用。它是 JUnit 4.x 包中不存在的 class。我 google 这个 class 加上像这样的 maven 这个词: org.junit.contrib.java.lang.system.StandardOutputStreamLog() + maven 我发现它没有在 junit Maven 存储库中定义,而是在 com.github.stefanbirkner .所以通过添加maven坐标

<dependency>
    <groupId>com.github.stefanbirkner</groupId>
    <artifactId>system-rules</artifactId>
    <version>1.16.0</version>
</dependency>

您应该可以解决问题。

如果您使用 gradle,您可以在 build.gradle 文件中添加以下依赖规则。

dependencies {
testCompile "com.github.stefanbirkner:system-rules:${systemRulesVersion}"}

您应该可以解决问题。