Could you explain me please how to mock System.in
correctly?
I have a simple test and I assign values to
@Test
public void testBarCorrect() {
System.setIn(new ByteArrayInputStream("7\n2\n3".getBytes()));
someService().consume();
}
Under the hood I do nothing but new Scanner(System.in);
The caveat is that sometimes my test is green but in 80% it throws an Exception "java.util.NoSuchElementException: No line found".
So, if I press run on a test it misbehaves.
Could you give me any hints?
My test:
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);
}
}
And exception occurs in run method in:
if (stdin == null) {
stdin = new Scanner(System.in);
}
String ans = stdin.nextLine();
someService
's code is kind of crucial here. It works alright for me. – Whorton