2017-09-27 154 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