Skip to content

Commit c902fc9

Browse files
committed
Update SerializationUnitTest.java
Changes to add more tests.
1 parent 09f205b commit c902fc9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

core-java-modules/core-java/src/test/java/com/baeldung/serialization/SerializationUnitTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import static org.junit.Assert.assertTrue;
44
import static org.junit.Assert.assertFalse;
55

6+
import java.io.FileInputStream;
67
import java.io.FileOutputStream;
78
import java.io.IOException;
89
import java.io.NotSerializableException;
10+
import java.io.ObjectInputStream;
911
import java.io.ObjectOutputStream;
1012
import java.io.Serializable;
1113

@@ -23,6 +25,25 @@ public void whenSerializing_ThenThrowsError() throws IOException {
2325
objectOutputStream.writeObject(address);
2426
}
2527
}
28+
29+
@Test
30+
public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
31+
Person p = new Person();
32+
p.setAge(20);
33+
p.setName("Joe");
34+
35+
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
36+
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
37+
objectOutputStream.writeObject(p);
38+
}
39+
40+
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
41+
try ( ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
42+
Person p2 = (Person) objectInputStream.readObject();
43+
assertTrue(p2.getAge() == p.getAge());
44+
assertTrue(p2.getName().equals(p.getName()));
45+
}
46+
}
2647

2748
@Test(expected = ClassCastException.class)
2849
public void whenSerializingUsingApacheCommons_ThenThrowsError() {

0 commit comments

Comments
 (0)