File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
core-java-modules/core-java/src/test/java/com/baeldung/serialization Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 33import static org .junit .Assert .assertTrue ;
44import static org .junit .Assert .assertFalse ;
55
6+ import java .io .FileInputStream ;
67import java .io .FileOutputStream ;
78import java .io .IOException ;
89import java .io .NotSerializableException ;
10+ import java .io .ObjectInputStream ;
911import java .io .ObjectOutputStream ;
1012import 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 () {
You can’t perform that action at this time.
0 commit comments