Merge pull request #8049 from vatsalgosar/BAEL-3143

BAEL 3143
This commit is contained in:
Eric Martin 2019-11-01 23:49:50 -05:00 committed by GitHub
commit d47ea19834
1 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package com.baeldung.scanner;
import static org.junit.Assert.assertEquals;
import java.util.NoSuchElementException;
import java.util.Scanner;
import org.junit.Test;
public class JavaScannerUnitTest {
@Test
public void whenReadingLines_thenCorrect() {
String input = "Scanner\nTest\n";
try (Scanner scanner = new Scanner(input)) {
assertEquals("Scanner", scanner.nextLine());
assertEquals("Test", scanner.nextLine());
}
}
@Test
public void whenReadingPartialLines_thenCorrect() {
String input = "Scanner\n";
try (Scanner scanner = new Scanner(input)) {
scanner.useDelimiter("");
scanner.next();
assertEquals("canner", scanner.nextLine());
}
}
@Test(expected = NoSuchElementException.class)
public void givenNoNewLine_whenReadingNextLine_thenThrowNoSuchElementException() {
try (Scanner scanner = new Scanner("")) {
String result = scanner.nextLine();
}
}
@Test(expected = IllegalStateException.class)
public void givenScannerIsClosed_whenReadingNextLine_thenThrowIllegalStateException() {
Scanner scanner = new Scanner("");
scanner.close();
String result = scanner.nextLine();
}
}