System.out.print() 在测试方法中没有显示任何内容
System.out.print() doesn't show anything in test methods
我试图在我的单元测试(@Test
方法)中使用 System.out
打印一些数据,但它没有显示任何内容。但是,它在 @Before
方法中正常工作。我将 JUnit 与 Maven Surefire 插件一起使用。
public class MyTests {
@Before
void init(){
System.out.println("Initializing some data..."); // <- It works.
}
@Test
void shouldRemoveSeries() {
System.out.println("TEST: Should remove series"); // <- It doesn't.
}
}
maven-surefire-plugin
配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
谢谢。
使用Log
private static Logger log = Logger.getLogger(LoggingObject.class);
log.info("I'm starting");
private final PrintStream stdout = System.out;
private final ByteArrayOutputStream output = new ByteArrayOutputStream();
private TerminalView terminalview;
问题是您的测试名称 class。要在构建的测试阶段被识别(由 Maven surefire 插件),它必须命名为“*Test”:
我觉得这听起来很熟悉,所以我假设您是 运行 来自某些 IDE(Netbeans?)的测试。它可能只显示 失败 的测试的输出。 运行 从控制台进行测试时也会出现这种情况吗?
使用 System.err
可能比 System.out
更幸运,但我不确定这一点。
要通过 System.out.println 获取书面测试的输出,您需要配置 maven-surefire-plugin 以将此输出重定向到一个文件,这可以通过以下方式实现:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
选项redirectTestOutputToFile会将System.out.println等的输出重定向到单独创建的文件中:
文档摘录:
Set this to "true" to redirect the unit test standard output to a file
(found in reportsDirectory/testName-output.txt).
除此之外,System.out.println 通常在单元测试中没有意义。
运行 入此也。我正在使用 gradle 来管理我的任务,我把它放在 build.gradle
文件的末尾:
test {
testLogging.showStandardStreams = true
}
现在我看到了System.out.println(whateves)
。
我在单独的非测试中做了个小动作class。它不像记录器那么流畅,但如果您正在寻找 Spring 引导中的快速解决方案,您可以使用它。
PrintForTest.java
import org.springframework.stereotype.Controller;
@Controller
public class PrintForTest {
public static void print(String input){
System.out.println(input);
}
}
MainTest.java
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.Assert;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MainTest {
...
@Test
public void testingSomething(){
PrintForTest.print("My new System.out.print()");
Assert.assertEquals(...);
}
}
已编辑:使用静态方法,无需使用@Autowired。
我正在使用 gradle
。 System.out
和 java.util.logging.Logger
都有这个问题。我编辑了 build.gradle
文件的以下部分:
test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
并在 testLogging
下添加了 showStandardStreams = true
。结果如下:
test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
showStandardStreams = true
}
}
它修复了它们。
Maven 的-Dtest=*
命令行选项似乎会触发单元测试中stdout 的显示。
按照惯例,标准输出显示在 target/surefire-reports/*-output.txt
中。显然,Surefire 插件开发人员无法重用标准输出来在测试和框架之间进行许多通信。
我试图在我的单元测试(@Test
方法)中使用 System.out
打印一些数据,但它没有显示任何内容。但是,它在 @Before
方法中正常工作。我将 JUnit 与 Maven Surefire 插件一起使用。
public class MyTests {
@Before
void init(){
System.out.println("Initializing some data..."); // <- It works.
}
@Test
void shouldRemoveSeries() {
System.out.println("TEST: Should remove series"); // <- It doesn't.
}
}
maven-surefire-plugin
配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
谢谢。
使用Log
private static Logger log = Logger.getLogger(LoggingObject.class);
log.info("I'm starting");
private final PrintStream stdout = System.out;
private final ByteArrayOutputStream output = new ByteArrayOutputStream();
private TerminalView terminalview;
问题是您的测试名称 class。要在构建的测试阶段被识别(由 Maven surefire 插件),它必须命名为“*Test”:
我觉得这听起来很熟悉,所以我假设您是 运行 来自某些 IDE(Netbeans?)的测试。它可能只显示 失败 的测试的输出。 运行 从控制台进行测试时也会出现这种情况吗?
使用 System.err
可能比 System.out
更幸运,但我不确定这一点。
要通过 System.out.println 获取书面测试的输出,您需要配置 maven-surefire-plugin 以将此输出重定向到一个文件,这可以通过以下方式实现:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
选项redirectTestOutputToFile会将System.out.println等的输出重定向到单独创建的文件中:
文档摘录:
Set this to "true" to redirect the unit test standard output to a file (found in reportsDirectory/testName-output.txt).
除此之外,System.out.println 通常在单元测试中没有意义。
运行 入此也。我正在使用 gradle 来管理我的任务,我把它放在 build.gradle
文件的末尾:
test {
testLogging.showStandardStreams = true
}
现在我看到了System.out.println(whateves)
。
我在单独的非测试中做了个小动作class。它不像记录器那么流畅,但如果您正在寻找 Spring 引导中的快速解决方案,您可以使用它。
PrintForTest.java
import org.springframework.stereotype.Controller;
@Controller
public class PrintForTest {
public static void print(String input){
System.out.println(input);
}
}
MainTest.java
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.Assert;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MainTest {
...
@Test
public void testingSomething(){
PrintForTest.print("My new System.out.print()");
Assert.assertEquals(...);
}
}
已编辑:使用静态方法,无需使用@Autowired。
我正在使用 gradle
。 System.out
和 java.util.logging.Logger
都有这个问题。我编辑了 build.gradle
文件的以下部分:
test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
并在 testLogging
下添加了 showStandardStreams = true
。结果如下:
test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
showStandardStreams = true
}
}
它修复了它们。
Maven 的-Dtest=*
命令行选项似乎会触发单元测试中stdout 的显示。
按照惯例,标准输出显示在 target/surefire-reports/*-output.txt
中。显然,Surefire 插件开发人员无法重用标准输出来在测试和框架之间进行许多通信。