【问题标题】:How to test output/input in java如何在java中测试输出/输入
【发布时间】:2015-03-16 10:43:51
【问题描述】:

我知道这可能是重复的(如果是这种情况,请原谅我),这两者中的任何一个都可能与我正在搜索的内容重复,但我不知道他们是否确实回答了我的有问题我还是在这里问吧。

JUnit test for console input and output,
JUnit test for System.out.println()

我的作业(学校):

我们要编写一个简单的编译器来跟踪铅笔的颜色以及在 x 轴和 y 轴上的移动。即

输入:

% 这是评论
% 现在我们正在制作一个正方形
下来。
前 1. 左 90.
前 1. 左 90.
前 1. 左 90.
前 1. 左 90。

输出:

#0000FF 0.0000 0.0000 1.0000 0.0000
#0000FF 1.0000 0.0000 1.0000 1.0000
#0000FF 1.0000 1.0000 0.0000 1.0000
#0000FF 0.0000 1.0000 0.0000 0.0000

我的问题:

为了获得及格分数,我们的代码要经过一系列严格的测试,其中许多测试无法让您知道哪里出了问题。即

“1215 字节的随机生成程序,有 1 个语法错误”

所以我觉得这项任务的很大一部分是学习如何编写好的测试代码,我一直在考虑尝试使用 JUnit(或者我应该使用其他东西?)。我的输入来自 System.in,输出通过 System.out.println()。

希望有一种方法可以通过使用现有的文本文件来做到这一点(之前使用 assertEquals 和充满 \n 的超长字符串遇到了一些困难)。澄清一下:我想测试我的 Java 程序的 I/O(通过文本文件),但我找不到任何关于如何做到这一点的明确解释。

【问题讨论】:

    标签: java testing junit io


    【解决方案1】:

    编写单元测试最重要的部分是将要测试的代码与给定框架中的样板/经过良好测试的代码分开 - 因此您可以创建创建输出的类,但将读取输入的部分与 @987654321 分开@ 并从您的业务逻辑中将其写入 System.out 以专注于“严重”部分。

    【讨论】:

      【解决方案2】:

      我最近在重构一些遗留代码时遇到了这个问题。我将输入/输出提取到新类中,并使它们依赖于我的程序。使用依赖注入,我插入了 mocks/fakes,这样我就可以定义输入和输出应该是什么。

      public class InputReader {
      
          private static final Scanner SCANNER = new Scanner(System.in);
      
          public String nextLine() {
              return SCANNER.nextLine();
          }
      
      }
      
      public class OutputPrinter {
      
          public void println(String string) {
              System.out.println(string);
          }
      
      }
      

      然后使用mockito进行测试:

      public class MainProgramTest {
      
          private final InputReader inputReader = mock(InputReader.class);
          private final OutputPrinter outputPrinter = mock(OutputPrinter.class);
      
          @Test
          public void playsSimpleGame() {
              when(inputReader.nextLine())
                      .thenReturn("hello")
                      .thenReturn("help")
                      .thenReturn("exit");
      
              MainProgram mainProgram = new MainProgram(inputReader, outputPrinter);
              mainProgram.run();
      
              InOrder inOrder = inOrder(inputReader, outputPrinter);
              inOrder.verify(inputReader).nextLine(); // -> "hello"
              inOrder.verify(outputPrinter).println("\"hello\" is not recognised command, type \"help\" for known commands");
              inOrder.verify(inputReader).nextLine(); // -> "help"
              inOrder.verify(outputPrinter).println("This is help, the only known commands are \"help\" and \"exit\"");
              inOrder.verify(inputReader).nextLine(); // -> "exit"
              inOrder.verify(outputPrinter).println("Goodbye");
              verifyNoMoreInteractions(inputReader, outputPrinter);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2012-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-24
        相关资源
        最近更新 更多