File tree Expand file tree Collapse file tree
core-java-modules/core-java-io-2/src/test/java/com/baeldung/scanner Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .scanner ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+
5+ import java .util .NoSuchElementException ;
6+ import java .util .Scanner ;
7+
8+ import org .junit .Test ;
9+
10+ public class JavaScannerUnitTest {
11+
12+ @ Test
13+ public void whenReadingLines_thenCorrect () {
14+ String input = "Scanner\n Test\n " ;
15+ try (Scanner scanner = new Scanner (input )) {
16+ assertEquals ("Scanner" , scanner .nextLine ());
17+ assertEquals ("Test" , scanner .nextLine ());
18+ }
19+ }
20+
21+ @ Test
22+ public void whenReadingPartialLines_thenCorrect () {
23+ String input = "Scanner\n " ;
24+ try (Scanner scanner = new Scanner (input )) {
25+ scanner .useDelimiter ("" );
26+ scanner .next ();
27+ assertEquals ("canner" , scanner .nextLine ());
28+ }
29+ }
30+
31+ @ Test (expected = NoSuchElementException .class )
32+ public void givenNoNewLine_whenReadingNextLine_thenThrowNoSuchElementException () {
33+ try (Scanner scanner = new Scanner ("" )) {
34+ String result = scanner .nextLine ();
35+ }
36+ }
37+
38+ @ Test (expected = IllegalStateException .class )
39+ public void givenScannerIsClosed_whenReadingNextLine_thenThrowIllegalStateException () {
40+ Scanner scanner = new Scanner ("" );
41+ scanner .close ();
42+ String result = scanner .nextLine ();
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments