[BAEL-19088] - Move articles out of core-java-io part 3

This commit is contained in:
catalin-burcea
2019-11-15 11:49:14 +02:00
parent 0950c53112
commit 5ec3630590
95 changed files with 114 additions and 180090 deletions

View File

@@ -0,0 +1,44 @@
package com.baeldung.scannernextline;
import org.junit.Test;
import java.util.NoSuchElementException;
import java.util.Scanner;
import static org.junit.Assert.assertEquals;
public class ScannerNextLineUnitTest {
@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();
}
}