我想编写一个JUnit测试来验证特定对象是否可以成功地通过GWT RPC过程进行序列化。我该怎么做?我使用的是GWT 2.4和JUnit 4.8.1。
谢谢,- Dave
谢谢,- Dave
被接受的答案没有检查无参数构造函数; 下面的代码有:
public static void checkGwtSerializability(Object o) throws RuntimeException {
HostedModeClientOracle hmco = new HostedModeClientOracle();
HasValues command = new ReturnCommand();
HasValuesCommandSink hvcs = new HasValuesCommandSink(command);
CommandServerSerializationStreamWriter out = new CommandServerSerializationStreamWriter(hmco, hvcs);
try {
out.writeObject(o);
} catch (SerializationException e) {
throw new RuntimeException("Object not serializable: " + o + " Caused by: " + e.getMessage(), e);
}
try {
o.getClass().getDeclaredConstructor();
} catch (NoSuchMethodException e) {
throw new RuntimeException("Object not serializable: " + o + " Caused by: " + e.getMessage(), e);
}
}
HostedModeClientOracle
,HasValues
,HasValuesCommandSink
和CommandServerSerializationStreamWriter
。 - sixtyfootersdude找到答案了。它是
@Test
public void testObjSerializability() {
final Object obj = getObject();
final HostedModeClientOracle hmco =new HostedModeClientOracle();
final HasValues command = new ReturnCommand();
final HasValuesCommandSink hvcs = new HasValuesCommandSink(command);
final CommandServerSerializationStreamWriter out = new CommandServerSerializationStreamWriter(hmco, hvcs);
try {
out.writeObject(obj);
} catch (Exception e) {
e.printStackTrace(System.err);
fail("Object couldn't be serialized:" + e.getMessage());
}
}
public class MyClass implements IsSerializable {
private File file;
public MyClass(String fileName) {
this.file = new File(fileName);
}
}
尽管 https://developers.google.com/web-toolkit/doc/1.6/DevGuideServerCommunication#DevGuideSerializableTypes 要求一个无参构造函数。这里有什么问题? - AlexHostedModeClientOracle
,HasValues
,HasValuesCommandSink
和CommandServerSerializationStreamWriter
。 - sixtyfootersdude