Skip to content

Commit d172d2d

Browse files
Ciaomaozhendrikse
authored andcommitted
fixes serialization unit test (eugenp#1837)
* adds serialization code * fixes serialization unit test
1 parent 3504296 commit d172d2d

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeuldung.serialization;
2+
import java.io.Serializable;
3+
4+
public class Person implements Serializable {
5+
6+
/**
7+
*
8+
*/
9+
private static final long serialVersionUID = 1L;
10+
11+
private int age;
12+
private String name;
13+
14+
public int getAge() {
15+
return age;
16+
}
17+
18+
public void setAge(int age) {
19+
this.age = age;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(String name) {
27+
this.name = name;
28+
}
29+
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeuldung.serialization;
2+
import static org.junit.Assert.assertEquals;
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.ObjectInputStream;
9+
import java.io.ObjectOutputStream;
10+
11+
import org.junit.Test;
12+
13+
public class PersonTest {
14+
15+
@Test
16+
public void testPeron() throws IOException, ClassNotFoundException {
17+
Person p = new Person();
18+
p.setAge(20);
19+
p.setName("Joe");
20+
21+
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
22+
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
23+
objectOutputStream.writeObject(p);
24+
objectOutputStream.flush();
25+
objectOutputStream.close();
26+
27+
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
28+
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
29+
Person p2 = (Person) objectInputStream.readObject();
30+
objectInputStream.close();
31+
assertTrue(p2.getAge() == p.getAge());
32+
assertTrue(p2.getName().equals(p.getName()));
33+
}
34+
35+
}

0 commit comments

Comments
 (0)