[英]Method assertEquals(Object, object) in the type Assert is not applicable for the arguments (String, Void)
I have a static void method that prints out a statement depending on what string is input and then returns. 我有一个静态的void方法,该方法根据输入的字符串打印出一条语句,然后返回。 I'm trying to use jUnit to make sure that the print statement is correct for the input given.
我正在尝试使用jUnit来确保print语句对于给定的输入是正确的。
I tried to use assertEquals(expected, System.method("input")); 我试图使用assertEquals(expected,System.method(“ input”)));
I am given the error "The method assertEquals(Object, object) in the type Assert is not applicable for the arguments (String, Void)." 我收到错误消息“ Assert类型中的方法assertEquals(Object,object)不适用于参数(String,Void)。” I understand the error, but I have been unable to find out how to write my test case differently so that I can compare the two.
我理解该错误,但是我一直无法找出如何以不同的方式编写测试用例,以便可以将两者进行比较。
To expand on @tradeJmark's answer; 扩展@tradeJmark的答案; assuming your static method (the one to test) is calling
System.out.print*
for something; 假设您的静态方法(要测试的方法)正在调用
System.out.print*
进行某些操作; then you can 'hijack' the OutputStream
for System
via: 那么您可以通过以下方式“劫持”
System
的OutputStream
:
@Test
public void testThing() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
ClassToTest.testMethod(/* desired input */);
final String written = out.toString();
Assert.assertEquals(expected, written);
}
Noting of course things like calling System.out.println(...)
(rather than System.out.print(...)
) will append \\n
(or similar depending on OS) to the end of the written String
, etc. 当然要注意,诸如调用
System.out.println(...)
(而不是System.out.print(...)
)之类的操作会将\\n
(或类似,具体取决于OS)附加到写入的String
,等等。 。
Note: If you're planning to do this; 注意:如果您打算这样做,请执行以下操作: I'd recommend reverting it somewhere as well.
我建议也将其还原到某个位置。 Something like the following should handle this.
像下面这样的东西应该可以解决这个问题。
private static PrintStream ORIGINAL_OUT = null;
@BeforeClass
public static void interceptOut() {
ORIGINAL_OUT = System.out;
}
@AfterClass
public static void revertOut() {
System.setOut(ORIGINAL_OUT);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.