包含扫描仪(TestNG)
问题描述:
的测试方法我坚持使用需要用户输入的测试方法。这里是我的方法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内部提供此扫描器。期待有关它的任何提示。提前致谢!
答
可惜这是你需要的信息,存根或使用一个框架,它存根为你
尝试的Mockito
这是用java开发普通问题的实例。