请你解释一下如何正确地模仿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);
}
}中的run方法中出现异常:
if (stdin == null) {
stdin = new Scanner(System.in);
}
String ans = stdin.nextLine();

发布于 2018-09-20 20:43:36
这个测试对我来说很好:
@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规则。
https://stackoverflow.com/questions/52432876
复制相似问题