您能解释一下如何正确模拟System.in
吗?
我有一个简单的测试,我将值分配给
@Test
public void testBarCorrect() {
System.setIn(new ByteArrayInputStream("7\n2\n3".getBytes()));
someService().consume();
}
在引擎盖下,我什么也没做,new Scanner(System.in);
警告是有时我的测试是绿色的,但在80%的测试中会抛出Exception "java.util.NoSuchElementException: No line found".
因此,如果我在测试中按run键,则行为不当。
您能给我些提示吗?
我的测试:
public class RecordServiceCTLIntegrationTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
private static final InputStream DEFAULT_STDIN = System.in;
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setIn(DEFAULT_STDIN);
}
@After
public void rollbackChangesToStdin() {
System.setOut(originalOut);
System.setIn(DEFAULT_STDIN);
}
@Test
public void testBarCorrect() {
System.setIn(new ByteArrayInputStream("5\n1\nB\n21\n\n".getBytes()));
buildRecordServiceCTL().run();
assertEquals("Enter Room Id: No active booking for room id: 5\n" +
"Enter Room Id: B:\tBar Fridge\n" +
"R:\tRestaurant\n" +
"S:\tRoom Service\n" +
"Enter service typeEnter cost: Service Bar Fridge has been added for the roomNumber 1\n" +
"Hit <enter> to continuePay for service completed\n", outContent.toString());
}
@Test
public void testRestaurantCorrect() {
System.setIn(new ByteArrayInputStream("5\n1\nR\n21\n\n".getBytes()));
buildRecordServiceCTL().run();
assertEquals("Enter Room Id: No active booking for room id: 5\n" +
"Enter Room Id: B:\tBar Fridge\n" +
"R:\tRestaurant\n" +
"S:\tRoom Service\n" +
"Enter service typeEnter cost: Service Restaurant has been added for the roomNumber 1\n" +
"Hit <enter> to continuePay for service completed\n", outContent.toString());
}
@Test
public void testRoomServiceCorrect() {
System.setIn(new ByteArrayInputStream("5\n1\nS\n21\n\n".getBytes()));
buildRecordServiceCTL().run();
assertEquals("Enter Room Id: No active booking for room id: 5\n" +
"Enter Room Id: B:\tBar Fridge\n" +
"R:\tRestaurant\n" +
"S:\tRoom Service\n" +
"Enter service typeEnter cost: Service Room Service has been added for the roomNumber 1\n" +
"Hit <enter> to continuePay for service completed\n", outContent.toString());
}
@Test(expected = NoSuchElementException.class)
public void testRoomException() {
System.setIn(new ByteArrayInputStream("-1\n\n".getBytes()));
buildRecordServiceCTL().run();
}
@Test
public void cancel() {
System.setIn(new ByteArrayInputStream("5\n1\nS\n20\n".getBytes()));
assertEquals("", outContent.toString());
}
private RecordServiceCTL buildRecordServiceCTL() {
Hotel hotel = new Hotel();
final Guest guest = new Guest("name", "address", 123);
final Room room = new Room(1, SINGLE);
final CreditCard card = new CreditCard(VISA, 123, 123);
final Booking booking = new Booking(guest, room, new Date(), 10, 1, card);
hotel.activeBookingsByRoomId.put(room.getId(), booking);
final Map<Integer,Room> map = new HashMap<>();
map.put(room.getId(), room);
hotel.roomsByType.put(SINGLE, map);
return new RecordServiceCTL(hotel);
}
}
在以下位置的运行方法中发生异常:
if (stdin == null) {
stdin = new Scanner(System.in);
}
String ans = stdin.nextLine();
答案 0 :(得分:3)
此测试对我来说很好:
@Test
public void testBarCorrect() {
System.setIn(new ByteArrayInputStream("7\n2\n3".getBytes()));
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());
System.out.println(scanner.nextLine());
System.out.println(scanner.nextLine());
}
但是,如果我添加以下测试,则会失败:
@Test
public void testFooCorrect() {
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());
System.out.println(scanner.nextLine());
System.out.println(scanner.nextLine());
}
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at com.amazon.adcs.service.dra.domain.A9DeviceTest.testFooCorrect(A9DeviceTest.java:15)
理论上,单元测试应使JVM保持与执行测试之前相同的状态。这不是您的情况,因为您已经更改了单例状态,并且在测试后不回滚所做的修改。如果您使用System.in
进行了多个测试,则第一个测试可能会消耗掉所有测试,而对其他测试则一无所有。
通常,我建议您在课堂上插入一个InputStream
,然后您就可以在不影响系统常量的情况下自由地进行任何操作。既然您告诉我您无法编辑代码,那么您应该确保自己清理状态。
类似这样的方法可以解决问题:
private static final InputStream DEFAULT_STDIN = System.in;
@After
public void rollbackChangesToStdin() {
System.setIn(DEFAULT_STDIN);
}
如果您必须经常执行此操作,则可能需要考虑实施JUnit rule。