2017-07-06 82 views
0

的測試方法我堅持使用需要用戶輸入的測試方法。這裏是我的方法IOHandler類:包含掃描儀(TestNG)

Callable<Integer> getNumber = new Callable<Integer>() { 
    @Override 
    public Integer call() throws Exception { 
     try { 
      return scanner.nextInt(); 
     } catch (InputMismatchException e) { 
      writeOut("Bad params !\n"); 
      scanner.nextLine(); 
     } 
     return call(); 
    } 
}; 

Optional<Object> handleIOAndGetInput(Callable function) { 
    try { 
     return Optional.ofNullable(function.call()); 
    } catch (IOException e) { 
     System.err.println("Stream doesn't work"); 
     e.printStackTrace(); 
     System.exit(1); 
    } catch (Exception e) { 
     System.err.println("Something went wrong"); 
     e.printStackTrace(); 
     System.exit(1); 
    } 
    return Optional.empty(); 
} 

這方法在我的測試類:

@Test 
public void shouldTakeUserInput() { 
    IOHandler ioHandler=new IOHandler(); 
    Assert.assertEquals((ioHandler.handleIOAndGetInput(ioHandler.getNumber)).orElse(null), 5); 
} 

所以我紅一些類似的帖子,但我cound't找到answear我的問題。我所做的一切都是關於將輸入信息僞裝成assert,但在我的情況下,我必須在Callable getNumber內部提供此掃描器。期待有關它的任何提示。提前致謝!

回答

0

可惜這是你需要的信息,存根或使用一個框架,它存根爲你

嘗試的Mockito

這是用java開發普通問題的實例。