简体   繁体   English

如何测试仅打印消息的方法

[英]How to test method that only print out message

I have method that print winner in the class Game: 我在类游戏中有打印赢家的方法:

public void getWinner(String winner){

   System.out.println("WINNER IS " + winner);

}

How can I test this method so far I have: 到目前为止,我如何测试此方法:

Game gm = new Game(); // it is declared in @before

@test

public void test(){

  ByteArrayOutputStream outContent = new ByteArrayOutputSystea();

  System.setOut(new PrintStream(outContent));

  gm.getWinner(Bob);

  assertEquals("WINNER IS Bob",outContent.toString());

}

I have an error message that say 我有一条错误消息说

org.unit.ComparisonFailuter expected:<WINNER IS Bob[]> but was: <WINNER IS Bob[
]>

Well could you please give me a tip on how to test getWinner method 好吧,能否请您给我一些有关如何测试getWinner方法的提示

omg don't do it! 天哪,不要这样做! you don't have to test the println method. 您不必测试println方法。 guys from sun and oracle have already done that - you can be sure it works. 来自sun和oracle的人已经做到了-您可以确定它是有效的。 all you have to test is that you pass the right string to to that method. 您需要测试的是将正确的字符串传递给该方法。 so refactor your code and create a function that return the desired string and test only that method by simple string comparison 因此,重构代码并创建一个返回所需字符串的函数,并通过简单的字符串比较仅测试该方法

From the documentation : 文档中

public void println(String x)

Prints a String and then terminate the line. This method behaves as though it invokes print(String) and then println().

So when you print the line in the method, there's a line separator after it which is defined as so: 因此,当您在方法中打印行时,其后会有一个行分隔符,其定义如下:

The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\\n').

So you can either add a hardcoded line separator to your expected output, or you could use the following code to get the separator for the current system and append that.: 因此,您可以在预期的输出中添加硬编码的行分隔符,也可以使用以下代码获取当前系统的分隔符并将其附加。

System.getProperty("line.separator");

A mockist approach: 嘲讽的方法:

@Test
public void testGetWinner()
    {
    // setup: sut
    Game game = new Game();
    PrintStream mockPrintStream = EasyMock.createMock(PrintStream.class);
    System.setOut(mockPrintStream);

    // setup: data
    String theWinnerIs = "Bob";

    // setup: expectations
    System.out.println("WINNER IS " + theWinnerIs);

    // exercise
    EasyMock.replay(mockPrintStream);
    game.getWinner(theWinnerIs);

    // verify
    EasyMock.verify(mockPrintStream);
    }

Pro: You don't need to care what System.out.println() does, in fact if the implementation changes your test will still pass. 优点:您无需关心System.out.println()功能,实际上,如果实现更改,您的测试仍会通过。

I think you try to compare to strings with == when you should use .equals() . 我认为您应该在使用.equals()时尝试将其与==字符串进行比较。 The strings are stored ia constant pool, but in this case you read a string from somewhere else, which not nescessarily goes into the constant pool. 字符串存储在常量池中,但是在这种情况下,您会从其他地方读取字符串,而不必将其放入常量池。

Try 尝试

assertTrue(outContent.toString().equals("WINNER IS Bob"));

or whatever your testing library calls it. 或您的测试库称呼它。

which looks for the characters in the String instead of the memory address ("ref") of the String. 它在字符串中查找字符,而不是字符串的内存地址(“ ref”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM