这是我的测试:
@Test
public void testAddPaperConfirm()
{
String input = "P\n" +
"The Life of Geoff the Platypus";
InputStream testInput = new ByteArrayInputStream(input.getBytes());
System.setOut(new PrintStream(testOutput));
System.setIn(testInput);
testReviewSystem.main(new String[] {});
assertEquals(testOutput.toString(), "What do you want to do?\n" +
"O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\n" +
"What is the title of the paper?\n" +
"[Paper added]\n"
+ "What do you want to do?\n" +
"O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\n");
}
答案 0 :(得分:0)
失败是因为它们不相同。
看起来相同的两个字符串可能不同。有许多字节无法表示和显示。
例如ascii代码0和ascii代码1,它们看起来完全相同,但它们不是。
答案 1 :(得分:0)
我认为您在Windows上运行测试并输出\r\n
而不是\n
作为行分隔符。您可以通过将断言更改为以下代码来尝试此操作。
assertEquals(testOutput.toString(), "What do you want to do?\r\n" +
"O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\r\n" +
"What is the title of the paper?\r\n" +
"[Paper added]\r\n"
+ "What do you want to do?\r\n" +
"O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\r\n")
我编写了一个名为System Rules的测试库,可以更轻松地测试命令行应用程序。
public class TheTest {
@Rule
public final TextFromStandardInputStream systemInMock
= emptyStandardInputStream();
@Rule
public final SystemOutRule systemOutRule
= new SystemOutRule().enableLog();
@Test
public void testAddPaperConfirm() {
systemInMock.provideLines("P", "The Life of Geoff the Platypus");
testReviewSystem.main(new String[] {});
String output = systemOutRule.getLogWithNormalizedLineSeparator();
assertEquals(output, "What do you want to do?\n" +
"O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\n" +
"What is the title of the paper?\n" +
"[Paper added]\n"
+ "What do you want to do?\n" +
"O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\n");
}
}