2014-09-23 66 views
0

我需要單元測試寫在我的代碼中的'等待重啓'操作。代碼將等待用戶給出的指定超時值,以便服務器重新啓動並出現。超時值過期後,它將引發超時異常。如何模擬單元測試的服務器重啓任務 - Java?

爲了測試,我需要模擬服務器重啓的行爲。這真的有可能嗎?

注:我使用TestNG框架進行單元測試。

+0

你可以添加更多的細節?推測你的代碼是作爲客戶端運行的。它如何檢測到服務器已重新啓動? – DNA 2014-09-23 09:04:15

+0

它可以完成,但它取決於你已有的抽象。你可以發佈一個小片段,顯示你在哪裏? – 2014-09-23 09:05:20

回答

0

那麼,以簡單的方式,聽不同的端口或不同的網址。它模擬服務器已關閉。

您可以在線程中等待一段時間,然後將URL更改爲正確以模擬服務器UP!

0
Here is my code snippet.. I want to write a TestNG stub to test this method. 

    public Response execute() throws TaskException 
    { 
     Buffer theOutputBuffer = myWorkSpace.getOutputBuffer(); 
     OutputStream theOutputStream; 
     try 
     { 
      isRebooted = false; 

      theOutputStream = theOutputBuffer.getOutputStream(); 

      Date theStartDate = new Date(); 
      long theElapsedTime = 0; 
      while(!isRebooted) 
      { 
       if (theElapsedTime > getDTO().getTimeoutValue()) 
       { 
        throw new TaskException("Timed out waiting for reboot!"); 
       } 
       else 
       { 
        try 
        { 
         if (theConsole == null) 
         { 
          waitDirectly(mySession); 
         } 
         else 
         { 
          synchronized(mySession) 
          { 
           waitThroughConsole(); 
          } 
         } 

        } 
        finally 
        { 
         Date theDate = new Date(); 
         theElapsedTime = 
          (theDate.getTime() - theStartDate.getTime())/MILLI_SECS_PER_SEC; 
        } 
       } 
      } 
     } 
     catch (SessionException ex) 
     { 
      throw new TaskException (ex); 
     } 
     catch (ConsoleHijackException ex) 
     { 
      throw new TaskException (ex); 
     } 
     catch (BufferException ex) 
     { 
      throw new TaskException (ex); 
     } 
     catch (TimedoutException ex) 
     { 
      throw new TaskException (ex); 
     } 
     catch (InterruptedException ex) 
     { 
      if (myExecutionShell != null) 
      { 
       try 
       { 
        myExecutionShell.close(); 
       } 
       catch (InterruptedException e) 
       { 
        throw new TaskException(e); 
       } 
      } 
      throw new TaskException(ex); 
     } 
     catch (ConsoleException e) 
     { 
      throw new TaskException(e); 
     } 
     catch (ShellException ex) 
     { 
      throw new TaskException (ex); 
     } 
相關問題