diff --git a/core-java-modules/core-java-io-apis-2/pom.xml b/core-java-modules/core-java-io-apis-2/pom.xml
index 579fa4a589..976cbd62e4 100644
--- a/core-java-modules/core-java-io-apis-2/pom.xml
+++ b/core-java-modules/core-java-io-apis-2/pom.xml
@@ -81,6 +81,12 @@
7.1.0
test
+
+ org.assertj
+ assertj-core
+ 3.4.1
+ test
+
core-java-io-apis-2
@@ -94,4 +100,5 @@
5.9.3
-
\ No newline at end of file
+
+
diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstream/InputStreamUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstream/InputStreamUnitTest.java
index a1b0ef251c..e253690762 100644
--- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstream/InputStreamUnitTest.java
+++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstream/InputStreamUnitTest.java
@@ -1,6 +1,6 @@
package com.baeldung.inputstream;
-import static org.junit.jupiter.api.io.CleanupMode.ALWAYS;
+import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayInputStream;
import java.io.File;
@@ -20,21 +20,20 @@ import java.util.Map;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
-
public class InputStreamUnitTest {
- @Test
- public void givenAString_whenWrittenToFileInputStream_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {
+ @Test //givenAStringWrittenToAFile_whenReadByX_thenY
+ public void givenAStringWrittenToFile_whenReadWithFileInputStream_thenItShouldExitsInTheInputStream(@TempDir Path tempDir) throws IOException {
Path sampleOut = tempDir.resolve("sample-out.txt");
List lines = Arrays.asList("Hello. This is just a test. Good bye.");
Files.write(sampleOut, lines);
File sampleOutFile = sampleOut.toFile();
try (InputStream inputStream = new FileInputStream(sampleOutFile)) {
- Assert.assertTrue(readString(inputStream).contains(lines.get(0)));
+ assertThat(readString(inputStream)).contains(lines.get(0));
}
}
@Test
- public void givenAString_whenWrittenToFileInputStreamWithStringConstructor_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {
+ public void givenAStringWrittenToFile_whenReadWithFileInputStreamWithStringConstructor_thenItShouldExitsInTheInputStream(@TempDir Path tempDir) throws IOException {
Path sampleOut = tempDir.resolve("sample-out.txt");
String expectedText = "Hello. Hi.";
List lines = Arrays.asList(expectedText);
@@ -43,23 +42,40 @@ public class InputStreamUnitTest {
.getAbsolutePath();
try (FileInputStream fis = new FileInputStream(fileAbsolutePath)) {
int content;
- int availabeBytes = fis.available();
- Assert.assertTrue(availabeBytes > 0);
+ int availableBytes = fis.available();
+ Assert.assertTrue(availableBytes > 0);
StringBuilder actualReadText = new StringBuilder();
while ((content = fis.read()) != -1) {
actualReadText.append((char) content);
}
- Assert.assertTrue(actualReadText.toString()
- .contains(expectedText));
+ assertThat(actualReadText.toString()).contains(expectedText);
}
}
@Test
- public void givenAString_whenWrittenToByteArrayInputStream_thenShouldMatchWhenRead() throws IOException {
+ public void givenAByteArray_whenReadWithByteArrayInputStream_thenTheStringValueOfTheByteArrayShouldMatchTheExpectedString() throws IOException {
byte[] byteArray = { 104, 101, 108, 108, 111 };
try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray)) {
- String expected = readString(bais);
- Assert.assertEquals("hello", expected);
+ assertThat(readString(bais)).isEqualTo("hello");
+ }
+ }
+ @Test
+ public void givenKeyValuePairsWrittenInAFile_whenPassedInOutputStreams_thenThePairsShouldExistsInObjectInputStreams(
+ @TempDir Path tempDir) throws IOException, ClassNotFoundException {
+ Path sampleOut = tempDir.resolve("sample-out.txt");
+ File fileText = sampleOut.toFile();
+ //Serialize a Hashmap then write it into a file.
+ Map kv = new HashMap<>();
+ try (ObjectOutputStream objectOutStream = new ObjectOutputStream(new FileOutputStream(fileText))) {
+ kv.put("baseURL", "baeldung.com");
+ kv.put("apiKey", "this_is_a_test_key");
+ objectOutStream.writeObject(kv);
+ }
+ //Deserialize the contents of a file then transform it back into a Hashmap
+ try (ObjectInputStream input = new ObjectInputStream(new FileInputStream(fileText))) {
+ HashMap inputKv = (HashMap) input.readObject();
+ assertThat(kv.get("baseURL")).isEqualTo( inputKv.get("baseURL"));
+ assertThat(kv.get("apiKey")).isEqualTo( inputKv.get("apiKey"));
}
}
@@ -72,27 +88,4 @@ public class InputStreamUnitTest {
return sb.toString();
}
- @Test
- public void givenKeyValuePairs_whenWrittenInATextFile_thenSuccessWhenParsedWithObjectInputStream(
- @TempDir(cleanup = ALWAYS) Path tempDir) throws IOException, ClassNotFoundException {
- Path sampleOut = tempDir.resolve("sample-out.txt");
- File fileText = sampleOut.toFile();
- //Serialize a Hashmap then write it into a file.
- Map kv = new HashMap<>();
- try (FileOutputStream fileOutStream = new FileOutputStream(fileText)) {
- try (ObjectOutputStream objectOutStream = new ObjectOutputStream(fileOutStream)) {
- kv.put("baseURL", "baeldung.com");
- kv.put("apiKey", "this_is_a_test_key");
- objectOutStream.writeObject(kv);
- }
- }
- //Deserialize the contents of a file then transform it back into a Hashmap
- try (FileInputStream fileStream = new FileInputStream(fileText)) {
- try (ObjectInputStream input = new ObjectInputStream(fileStream)) {
- HashMap inputKv = (HashMap) input.readObject();
- Assert.assertEquals(kv.get("baseURL"), inputKv.get("baseURL"));
- Assert.assertEquals(kv.get("apiKey"), inputKv.get("apiKey"));
- }
- }
- }
}
diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstreamreader/InputStreamReaderUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstreamreader/InputStreamReaderUnitTest.java
index ea4572fa5f..2dfcc4f9e7 100644
--- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstreamreader/InputStreamReaderUnitTest.java
+++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstreamreader/InputStreamReaderUnitTest.java
@@ -1,5 +1,7 @@
package com.baeldung.inputstreamreader;
+import static org.assertj.core.api.Assertions.assertThat;
+
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
@@ -10,10 +12,8 @@ import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
-import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
-
public class InputStreamReaderUnitTest {
@Test
public void givenAStringWrittenToAFile_whenReadByInputStreamReader_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {
@@ -21,9 +21,9 @@ public class InputStreamReaderUnitTest {
String sampleTxt = "Good day. This is just a test. Good bye.";
Path sampleOut = tempDir.resolve("sample-out.txt");
List lines = Arrays.asList(sampleTxt);
- //create and write file
Files.write(sampleOut, lines);
- try (FileInputStream fis = new FileInputStream(String.valueOf(sampleOut.toAbsolutePath())); BufferedReader br = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8))) {
+ String absolutePath = String.valueOf(sampleOut.toAbsolutePath());
+ try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(absolutePath), StandardCharsets.UTF_8))) {
String ln;
while ((ln = br.readLine()) != null) {
if (ln.contains(sampleTxt)) {
@@ -31,7 +31,7 @@ public class InputStreamReaderUnitTest {
break;
}
}
- Assert.assertTrue(isMatched);
+ assertThat(isMatched).isTrue();
}
}
}