사용자가 ESC
을 입력 할 때 콘솔 앱의 TestNgtest 케이스를 쓰려고합니다. 어느 시점에서 응용 프로그램은 메시지를 인쇄 한 다음 종료해야합니다. 메시지가 인쇄되면 TestNg에서 테스트하도록합니다.출구가있는 TestNg 콘솔 앱
public class Application {
public static void doSomething(Scanner scanner) {
String inputString = scanner.nextLine();
if("ESC".equals(inputString.toUpperCase())) {
System.out.println("Bye");
System.exit(0);
}
}
}
다음은 JUnit을 코드입니다 :
public class ApplicationTest {
private Application app;
private ByteArrayInputStream in;
private ByteArrayOutputStream out;
@BeforeMethod
public void setUp() throws Exception {
app = new Application();
out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
}
@AfterMethod
public void tearDown() throws Exception {
System.setIn(System.in);
}
@Test
public void testESCInput() throws Exception {
in = new ByteArrayInputStream("ESC".getBytes());
System.setIn(in);
app.processInput(new Scanner(System.in));
assertTrue(out.toString().contains("Bye"));
}
}
을하지만 System.exit
와 응용 프로그램이 종료 나는 심지어 assertTrue
라인에 도착하지 않는 때문에, TestNG를 그냥 전에 종료 다음은 응용 프로그램 코드입니다. 이것을 시험 할 올바른 방법이 있습니까?
esc를 눌러 다른 클래스 (가능하면'Runnable ')에 넣으면 동작을 외부화 할 수 있습니다. 그런 다음 테스트 용 모의 구현을 제공 할 수 있습니다. –