Java编写单元测试,用于在控制台中退出用户类型时退出程序 [英] Java Writing unittest for exiting a program when user type quit in the console
问题描述
我发现很难为这种方法编写单元测试,它基本上在用户键入quit命令时退出程序.
I found really hard to write unit test for this method, it basically exits the program when user types a quit command.
SytemExit类:
SytemExit class:
public class SystemExit {
public void exit(int status) {
System.exit(status);
}
}
我的静态方法:
public static void exitWhenQuitDetected() {
final SystemExit systemExit = new SystemExit();
final String QUIT = "quit";
String line = "";
try {
final InputStreamReader input = new InputStreamReader(System.in);
final BufferedReader in = new BufferedReader(input);
while (!(line.equals(QUIT))) {
line = in.readLine();
if (line.equals(QUIT)) {
System.out.println("You are now quiting the program");
systemExit.exit(1);
}
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
这里有些不对劲,因为我正在努力对方法exitWhenQuitDetected进行单元测试(我正在使用Mockito进行模拟).当看到退出时,我将如何模拟InputStreamReader并验证SystemExit.exit方法是否被调用?您能对此点些照明吗?谢谢.
Something is not quite right here as I am struggling to unit test the method exitWhenQuitDetected (I am using Mockito for mocking). How would I mock the InputStreamReader and verify the SystemExit.exit method gets called when it sees a quit? Can you shed some lights on this please? Thanks.
添加了我目前正在进行的测试,它无法正常工作.
Added the test I am working on at the moment, it's not working.
@Test
@Ignore
public void shouldExitProgramWhenTypeQuit() {
String quit = "quit";
SystemExit systemExit = mock(SystemExit.class);
try {
BufferedReader bufferedReader = mock(BufferedReader.class);
when(bufferedReader.readLine()).thenReturn(quit + "\n");
SomeClass.exitWhenQuitDetected();
verify(systemExit, times(1)).exit(1);
} catch (IOException e) {
e.printStackTrace();
}
}
推荐答案
您已经完成了90%的工作,方法是将实际的退出代码放在没有自己逻辑的单独类中.您的困难是由使用静态方法引起的.
You've done 90% of the work already, by placing the actual exiting code off in a separate class with no logic of its own. Your difficulty is caused by your use of a static method.
我建议使exitWhenQuitDetected
不是静态的.将其放在您可以在需要时实例化的类中,并可以使用模拟的SystemExit
创建该类.像这样的东西.
I would advise making the exitWhenQuitDetected
not static. Put it in a class that you can instantiate when you need it, and that you can create with a mocked SystemExit
. Something like this.
public class SomeClass{
private final SystemExit exiter;
private final static String QUIT = "quit";
public SomeClass(){
this(new SystemExit());
}
SomeClass(SystemExit exiter){
this.exiter = exiter;
}
public static void exitWhenQuitDetected() {
String line = "";
try {
final InputStreamReader input = new InputStreamReader(System.in);
final BufferedReader in = new BufferedReader(input);
while (!(line.equals(QUIT))) {
line = in.readLine();
if (line.equals(QUIT)) {
System.out.println("You are now quiting the program");
exiter.exit(1);
}
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
// ...
}
然后,在您的测试中,您可以制作SystemExit
的模拟,并使用SomeClass
的程序包专用构造函数创建一个对象,该对象将您的模拟用作其exiter
.然后,您可以运行测试,并在模拟SystemExit
上运行verify
.
Then, in your test, you can make a mock of SystemExit
, and use the package-private constructor of SomeClass
to create an object that will use your mock as its exiter
. You can then run your test, and verify
on your mock SystemExit
.
这篇关于Java编写单元测试,用于在控制台中退出用户类型时退出程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!