2017-09-27 152 views
-3

这是我的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()方法

private void loadDescription() { 
     BufferedReader in = null; 
     StringBuffer stringBuffer = null; 
     String str = null; 
     try { 
      stringBuffer = new StringBuffer(); 
      in = new BufferedReader(new FileReader(IConstants.MANUAL_FILE)); 
      str = in.readLine(); 
      while (str != null) { 
       stringBuffer.append(str + "\n"); 
       str = in.readLine(); 
      } 
      dfixDescription = stringBuffer.toString(); 
     } catch (Exception e) { 
      logger.error(e.getMessage(), e); //To change body of catch statement use File | Settings | File Templates. 
     } finally { 
      try { 
       if (in != null) { 
        in.close(); 
       } 
      } catch (IOException e) { 
       logger.error(e.getMessage(), e); //To change body of catch statement use File | Settings | File Templates. 
      } 
     } 
    } 

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(); 

    } 

这是什么问题。当我运行测试方法时,它不会停止,我无法验证所需的方法是如何处理的。

+2

请勿将代码张贴为图片。直接将其作为文本发布在问题中。 – Carcigenicate

+0

另外“但是有了无限循环,我不能这样做。”有点含糊。你需要什么特别的帮助? – Carcigenicate

+0

您的代码的另一个问题是它要求用户输入。 – Zefick

回答

2

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

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

+0

@GhostCat:我已投票删除这篇文章。一个去。 – Bathsheba

+0

@GhostCat:我个人认为这是对人们说“不回答可怜的问题 - 你浪费自己的时间”的一种方式。在这种情况下,我当然是犯了罪。 – Bathsheba

+0

@GhostCat:只需要另一个问题upvote清除删除投票 - 永久。不错的SO臭虫,一个... – Bathsheba

0

显然你不能测试一个循环是否真的是“无限”。

但你仍然可以退后一步,看看所有这个方法是干什么的,喜欢的细节:

  • 打开一个读者从中会读
  • 具有特殊的条件下,确保“东西”只发生一次

换句话说:通常与单元测试一样,您要仔细考虑代码可以采用的不同路径(请考虑:“白盒测试”)。这可以告诉你关于触发角落案件所需的测试用例。另一方面,你也应该看看你的方法的公共合约(黑盒测试) - 你期望这种方法做什么事情,而不知道它的实现。这就是你如何看待你的代码,然后才能想出合理的测试。

除此之外:你的例子是假的。这里不应该有一个无限循环 - 在某些时候,所有的行都会被读取,并且read()不会返回其他任何东西,而是空的。

+0

我发布了我所有的代码,并且遇到while循环的问题。 – IsharaD