Skip to content

Commit a878222

Browse files
committed
BAEL-3143 - made the required changes
1 parent 9946fa5 commit a878222

1 file changed

Lines changed: 11 additions & 31 deletions

File tree

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,34 @@
11
package com.baeldung.scanner;
22

3-
import org.junit.Test;
3+
import static org.junit.Assert.assertEquals;
44

5-
import java.io.ByteArrayInputStream;
6-
import java.io.IOException;
7-
import java.io.InputStream;
8-
import java.nio.charset.StandardCharsets;
95
import java.util.NoSuchElementException;
106
import java.util.Scanner;
117

12-
import static org.assertj.core.api.Assertions.fail;
13-
import static org.junit.Assert.assertEquals;
8+
import org.junit.Test;
149

1510
public class JavaScannerUnitTest {
1611

1712
@Test
1813
public void whenReadingLines_thenCorrect() {
1914
String input = "Scanner\nTest\n";
20-
21-
byte[] byteArray = input.getBytes(StandardCharsets.UTF_8);
22-
//@formatter:off
23-
try (InputStream is = new ByteArrayInputStream(byteArray);
24-
Scanner scanner = new Scanner(is)) {
25-
26-
//@formatter:on
27-
String result = scanner.nextLine() + " " + scanner.nextLine();
28-
29-
String expected = input.replace("\n", " ")
30-
.trim();
31-
assertEquals(expected, result);
32-
} catch (IOException e) {
33-
fail(e.getMessage());
15+
try (Scanner scanner = new Scanner(input)) {
16+
assertEquals("Scanner", scanner.nextLine());
17+
assertEquals("Test", scanner.nextLine());
3418
}
3519
}
3620

3721
@Test(expected = NoSuchElementException.class)
38-
public void whenReadingLinesFromStringContainingNoLines_thenThrowNoSuchElementException() {
39-
String input = "";
40-
Scanner scanner = new Scanner(input);
41-
String result = scanner.nextLine();
42-
scanner.close();
22+
public void whenReadingLines_thenThrowNoSuchElementException() {
23+
try (Scanner scanner = new Scanner("")) {
24+
String result = scanner.nextLine();
25+
}
4326
}
4427

4528
@Test(expected = IllegalStateException.class)
46-
public void whenReadingLinesUsingClosedScanner_thenThrowIllegalStateException() {
47-
String input = "";
48-
Scanner scanner = new Scanner(input);
29+
public void whenReadingLines_thenThrowIllegalStateException() {
30+
Scanner scanner = new Scanner("");
4931
scanner.close();
5032
String result = scanner.nextLine();
5133
}
52-
53-
5434
}

0 commit comments

Comments
 (0)