Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无限循环的单元测试_Java_Junit_Mockito - Fatal编程技术网

Java 无限循环的单元测试

Java 无限循环的单元测试,java,junit,mockito,Java,Junit,Mockito,这是我的java线程运行方法,我想为这个方法编写单元测试。但有了英文特环,我无法做到这一点。如果有人能帮我,那就太好了 public void run() { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = null; boolean oneTime = true; loadDescription();

这是我的java线程运行方法,我想为这个方法编写单元测试。但有了英文特环,我无法做到这一点。如果有人能帮我,那就太好了

 public void run() {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        boolean oneTime = true;
        loadDescription();
        while (true) {

            try {
                if (oneTime) {
                    System.out.println("press enter to get the console...............");
                    oneTime = false;
                }
                line = in.readLine();
                if (line != null) {
                    processInput(line);
                }

            } catch (Exception e) {
                logger.error("Error occurred while processing console");
                logger.error(e.getMessage(), e);
            }
        }
    }
这是loadDescription()方法

这些是run方法的相关方法。以下是我的测试方法

 @Test
    public void run_activate() throws Exception{

        String data = "activate";
        InputStream in = new ByteArrayInputStream(data.getBytes());
        System.setIn(in);
        PowerMockito.mockStatic(AdminManager.class);
        PowerMockito.when(AdminManager.getInstance()).thenReturn(adminManagerTest);
        Mockito.doNothing().when(adminManagerTest).startDFIXRouter();
        dfixrtrAdminTest.run();
        Mockito.verify(adminManagerTest, Mockito.times(1)).startDFIXRouter();

    }

这有什么不对。当我运行测试方法时,它不会停止,并且我无法验证是否调用了所需的方法来处理此问题。

显然,您无法测试循环是否真的是“无限的”

但是,您仍然可以后退一步,查看此方法正在执行的所有细节,如:

  • 打开要从中读取的读取器
  • 具有确保“某事”只发生一次的特殊条件
换句话说:与通常的单元测试一样,您仔细考虑代码可以采用的不同路径(想想:“白盒测试”)。这可以告诉您需要什么样的测试用例才能成功。另一方面,您还应该查看方法的公共契约(黑盒测试)——在不知道其实现的情况下,您希望该方法做什么。这就是您对代码的思考方式,以便提出合理的测试


除此之外,你的例子是假的。这里不应该有一个无限循环-在某个点上,所有的行都将被读取,
read()
将不会返回除null以外的任何其他内容。

显然,您无法测试循环是否真的是“无限的”

但是,您仍然可以后退一步,查看此方法正在执行的所有细节,如:

  • 打开要从中读取的读取器
  • 具有确保“某事”只发生一次的特殊条件
换句话说:与通常的单元测试一样,您仔细考虑代码可以采用的不同路径(想想:“白盒测试”)。这可以告诉您需要什么样的测试用例才能成功。另一方面,您还应该查看方法的公共契约(黑盒测试)——在不知道其实现的情况下,您希望该方法做什么。这就是您对代码的思考方式,以便提出合理的测试


除此之外,你的例子是假的。这里不应该有无限循环-在某一点上,所有的行都将被读取,
read()
将不会返回除null以外的任何内容。

您需要用函数替换无限循环中的代码


然后为该函数编写单元测试。

需要用函数替换无限循环中的代码


然后为该函数编写单元测试。

不要将代码作为图片发布。直接在问题中以文本的形式发布。还有“但使用infinte循环我不能这样做。”有点含糊不清。您具体需要什么帮助?代码的另一个问题是它需要用户输入。现在是将代码重构为依赖项可注入和单元可测试组件的时候了。目前不是。我已经在我的问题中添加了我的代码。不要将代码作为图片发布。直接在问题中以文本的形式发布。还有“但使用infinte循环我不能这样做。”有点含糊不清。您具体需要什么帮助?代码的另一个问题是它需要用户输入。现在是将代码重构为依赖项可注入和单元可测试组件的时候了。目前不是。我已经在我的问题中添加了我的代码。我发布了我所有的代码,但是while循环有问题。我发布了我所有的代码,但是while循环有问题。
private void processInput(String line) {
        line = line.trim();
        logger.info("Admin Message received: " + line);
        if ("?".equalsIgnoreCase(line) || "".equals(line)) {
              printDescription();
        } else if (line.toUpperCase().startsWith(AdminCommands.RELOAD)) {
            loadDescription();
            printDescription();
        } else if (line.toUpperCase().startsWith(AdminCommands.EXIT)) {
            adminManager.stopDFIXRouter();
            logger.debug("Closing Application.....");
            logger.debug("Closed.");
            System.exit(0);
        } else if (line.toUpperCase().startsWith(AdminCommands.RESET_OUT_SEQUENCE)) {
            try {
                String sessionIdentifier = line.split(",")[1].trim();
                int seqNo = Integer.parseInt((line.split(",")[2]).trim());
                adminManager.resetOutSequence(sessionIdentifier, seqNo);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);  //To change body of catch statement use File | Settings | File Templates.
            }
        } else if (line.toUpperCase().startsWith(AdminCommands.RESET_IN_SEQUENCE)) {
            try {
                String sessionIdentifier = line.split(",")[1].trim();
                int seqNo = Integer.parseInt((line.split(",")[2]).trim());
                adminManager.resetInSequence(sessionIdentifier, seqNo);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);  //To change body of catch statement use File | Settings | File Templates.
            }
        } else if (line.toUpperCase().startsWith(AdminCommands.CONNECT)) {
            String sessionIdentifier = line.split(",")[1].trim();
            adminManager.connectSession(sessionIdentifier);
        } else if (line.toUpperCase().startsWith(AdminCommands.DISCONNECT)) {
            String sessionIdentifier = line.split(",")[1].trim();
            adminManager.disconnectSession(sessionIdentifier);
        } else if (line.toUpperCase().startsWith(AdminCommands.ACTIVATE)) {
            adminManager.startDFIXRouter();
        } else if (line.toUpperCase().startsWith(AdminCommands.PASSIVATE)) {
            adminManager.stopDFIXRouter();
        } else if (line.toUpperCase().startsWith(AdminCommands.RUN_EOD)) {
            try {
                String sessionIdentifier = line.split(",")[1].trim();
                adminManager.runEod(sessionIdentifier);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);  //To change body of catch statement use File | Settings | File Templates.
            }
        } else if (line.toUpperCase().startsWith(AdminCommands.SHOW_STATUS)) {
            adminManager.showStatus();
        } else {
            System.out.println("Usage: type ? for help");
        }
        System.out.print(">");
    }
 @Test
    public void run_activate() throws Exception{

        String data = "activate";
        InputStream in = new ByteArrayInputStream(data.getBytes());
        System.setIn(in);
        PowerMockito.mockStatic(AdminManager.class);
        PowerMockito.when(AdminManager.getInstance()).thenReturn(adminManagerTest);
        Mockito.doNothing().when(adminManagerTest).startDFIXRouter();
        dfixrtrAdminTest.run();
        Mockito.verify(adminManagerTest, Mockito.times(1)).startDFIXRouter();

    }