Testing GWT Serialization

I want to write a JUnit test to check if a particular object can be successfully serialized by the GWT RPC procedure. How can I do it? I am using GWT 2.4 and JUnit 4.8.1.

Thanks - Dave

+4
source share
2 answers

Found the answer.

@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()); } } 
+1
source

The accepted answer does not check the constructor no args; following:

 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); } } 
+4
source

Source: https://habr.com/ru/post/1381383/


All Articles