我正在尝试验证我的自定义Java对象是否正确序列化/反序列化,并且我可以对文件、流等使用这种序列化机制。这是我创建的测试。这是一种正确的方法吗?
public class SerializableTest {
@Test public void shouldSerialize() throws Exception {
Foo foo = new Foo(123);
// serialize it to a string
ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(foo);
String written = out.toString();
// read it back
ByteArrayInputStream in = new ByteArrayInputStream(written.getBytes());
Foo foo2 = (Foo)(new ObjectInputStream(in).readObject());
// check that two objects are identical
assertEquals(foo, foo2);
}
}
这里怎么了?