Java System.out.print() 在测试方法中不显示任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29488900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.out.print() doesn't show anything in test methods
提问by Héctor
I'm trying to print some data with System.out
in my unit tests (@Test
mehotds), but it is not showing anything. However, it works properly in @Before
method. I'm using JUnit with Maven Surefire plugin.
我试图System.out
在我的单元测试(@Test
mehotds)中打印一些数据,但它没有显示任何内容。但是,它在@Before
方法上正常工作。我正在使用带有 Maven Surefire 插件的 JUnit。
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
configuration:
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>
Thanks.
谢谢。
采纳答案by Jordi Castilla
回答by gclaussn
The problem is the name of your test class. To be recognized in the test phase within the build (by the Maven surefire plugin), it must be named "*Test":
问题是您的测试类的名称。为了在构建的测试阶段被识别(通过 Maven surefire 插件),它必须被命名为“*Test”:
回答by Waldheinz
This sound familiar to me, so I assume you're running your tests from some IDE (Netbeans?). It might be the case that it only shows the output for tests that fail. Does this also occur when running the test from console?
这对我来说听起来很熟悉,所以我假设您是从某个 IDE(Netbeans?)运行测试的。它可能只显示失败的测试的输出。从控制台运行测试时是否也会发生这种情况?
You might have more luck using System.err
instead of System.out
, but I'm not sure about this.
使用System.err
而不是你可能会更幸运System.out
,但我不确定这一点。
回答by khmarbaise
To get the output of your written Tests via System.out.println you need to configure maven-surefire-plugin to redirect this output into a file which can be achieved by using the following:
要通过 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>
The option redirectTestOutputToFilewill redirect the output of System.out.println etc. into a file which is separately created:
选项redirectTestOutputToFile会将 System.out.println 等的输出重定向到单独创建的文件中:
Excerpt from the docs:
摘自文档:
Set this to "true" to redirect the unit test standard output to a file (found in reportsDirectory/testName-output.txt).
将此设置为“true”以将单元测试标准输出重定向到一个文件(在reportsDirectory/testName-output.txt 中找到)。
Apart from that a System.out.println does not make sense in a unit test in general.
除此之外, System.out.println 通常在单元测试中没有意义。
回答by Patrick
Ran into this as well. I'm using gradle to manage my tasks and I put this in at the end of by build.gradle
file :
也遇到了这个。我正在使用 gradle 来管理我的任务,并将其放在build.gradle
文件末尾:
test {
testLogging.showStandardStreams = true
}
Now I see System.out.println(whateves)
.
现在我明白了System.out.println(whateves)
。
回答by Dev Man
I made i little trick in separate non-test class. It is not that smooth as logger, but if you are looking for quick solution in Spring Boot you can use this.
我在单独的非测试课上做了一些小把戏。它不像记录器那么流畅,但是如果您正在寻找 Spring Boot 中的快速解决方案,您可以使用它。
PrintForTest.java
打印测试.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(...);
}
}
edited: using static method, no need to use @Autowired.
编辑:使用静态方法,无需使用@Autowired。
回答by Abdollah
I am using gradle
. I had this problem with both System.out
and java.util.logging.Logger
. I edited the following part of my build.gradle
file:
我正在使用gradle
. 我有这个问题既System.out
和java.util.logging.Logger
。我编辑了build.gradle
文件的以下部分:
test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
and added showStandardStreams = true
under testLogging
. The result was as follows:
并showStandardStreams = true
在testLogging
. 结果如下:
test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
showStandardStreams = true
}
}
It fixed both of them.
它固定了他们两个。
回答by eel ghEEz
The -Dtest=*
command line option of Maven appears to trigger the show of stdout in unit tests.
-Dtest=*
Maven的命令行选项似乎触发了单元测试中 stdout 的显示。
By convention, the stdout shows in target/surefire-reports/*-output.txt
. Apparently, the Surefire plugin developers could not reuse stdout for communication of many things between the tests and the framework.
按照惯例,标准输出显示在target/surefire-reports/*-output.txt
. 显然,Surefire 插件开发人员无法重用 stdout 来在测试和框架之间进行许多事情的通信。