简体   繁体   English

声明类型中的方法assertEquals(Object,object)不适用于参数(String,Void)

[英]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: 那么您可以通过以下方式“劫持” SystemOutputStream

@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.

相关问题 类型PrintStream的方法printf(String,Object ...)不适用于参数(String,void) - The method printf(String, Object…) in the type PrintStream is not applicable for the arguments (String, void) ArrayList类型中的方法add(String) <String> 不适用于参数(对象) - The method add(String) in the type ArrayList<String> is not applicable for the arguments (Object) 字符串类型中的方法format(String,Object [])不适用于参数(…) - The method format(String, Object[]) in the type String is not applicable for the arguments (…) 类型object中的equals(object)方法不适用于参数 - the method equals(object) in the type object is not applicable to the arguments PrintStream 类型中的 printf(String, Object[]) 方法不适用于参数 (...) - The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (...) OptionTag类型的setValue(String)方法不适用于自变量(对象) - The method setValue(String) in the type OptionTag is not applicable for the arguments (Object) showMessageDialog错误:JOptionPane类型的showMessageDialog(Component,Object)方法不适用于参数(null,void) - Error with showMessageDialog:The method showMessageDialog(Component, Object) in the type JOptionPane is not applicable for the arguments (null, void) 错误:类型 Object 的方法 assertEquals(String, String) 未定义 - Error:The method assertEquals(String, String) is undefined for the type Object 验证器类型中的方法validate(capture#2-of?extended Object) <capture#2-of ? extends Object> 不适用于参数(字符串) - The method validate(capture#2-of ? extends Object) in the type Validator<capture#2-of ? extends Object> is not applicable for the arguments (String) 不明确的方法调用 Assert 中的 assertEquals(Object, Object) 和 Assert 中的 assertEquals(double, double) 匹配: - Ambiguous method call Both assertEquals(Object, Object) in Assert and assertEquals(double, double) in Assert match:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM