{"id":5327,"date":"2014-03-30T20:44:31","date_gmt":"2014-03-30T20:44:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/class-not-serializable-after-methods-are-overridden-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:44:31","modified_gmt":"2014-03-30T20:44:31","slug":"class-not-serializable-after-methods-are-overridden-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/class-not-serializable-after-methods-are-overridden-collection-of-common-programming-errors\/","title":{"rendered":"Class not serializable after methods are overridden-Collection of common programming errors"},"content":{"rendered":"<p>I override a <code>createSocket()<\/code> method in my test cases to pas in a mocked Socket. After doing this the objects aren&#8217;t serializable anymore.<\/p>\n<p>Here&#8217;s a example of what doesn&#8217;t work.<\/p>\n<p><strong>Foo.java<\/strong><\/p>\n<pre><code>import java.io.Serializable;\n\npublic class Foo implements Serializable {\n    private static final long serialVersionUID = 3109852436898487119L;\n\n    public void bar() {\n        System.out.println(\"Foo\");\n    }\n}\n<\/code><\/pre>\n<p><strong>FooTest.java<\/strong><\/p>\n<pre><code>import java.io.ByteArrayOutputStream;\nimport java.io.IOException;\nimport java.io.ObjectOutputStream;\n\nimport junit.framework.TestCase;\n\nimport org.junit.Test;\n\npublic class FooTest extends TestCase {\n\n    \/\/ this passes\n    @Test\n    public void testFooIsSerializable() throws IOException {\n        Foo foo = new Foo();\n\n        ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        ObjectOutputStream out = new ObjectOutputStream(baos);\n        out.writeObject(foo);\n    }\n\n    \/\/ this throws a java.io.NotSerializableException\n    @Test\n    public void testFooIsStillSerializableAfterBarIsOverridden()\n            throws IOException {\n\n        \/\/ Eclipse gives me the warning \"The serializable class  does not declare a static final serialVersionUID field of type long\"\n        \/\/ Adding it doesn't help\n        Foo foo = new Foo() {\n            @Override\n            public void bar() {\n                System.out.println(\"Bar\");\n            }\n        };\n\n        ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        ObjectOutputStream out = new ObjectOutputStream(baos);\n        out.writeObject(foo);\n    }\n}\n<\/code><\/pre>\n<p><strong>The stack trace when running FooTest with JUnit:<\/strong><\/p>\n<pre><code>java.io.NotSerializableException: FooTest\n    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)\n    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)\n    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)\n    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)\n    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)\n    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)\n    at FooTest.testFooIsStillSerializableAfterBarIsOverridden(FooTest.java:33)\n    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n    at java.lang.reflect.Method.invoke(Method.java:597)\n    at junit.framework.TestCase.runTest(TestCase.java:168)\n    at junit.framework.TestCase.runBare(TestCase.java:134)\n    at junit.framework.TestResult$1.protect(TestResult.java:110)\n    at junit.framework.TestResult.runProtected(TestResult.java:128)\n    at junit.framework.TestResult.run(TestResult.java:113)\n    at junit.framework.TestCase.run(TestCase.java:124)\n    at junit.framework.TestSuite.runTest(TestSuite.java:232)\n    at junit.framework.TestSuite.run(TestSuite.java:227)\n    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)\n    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)\n    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)\n    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)\n    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)\n    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)\n    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)\n<\/code><\/pre>\n<p>What is the problem with this? I have to admit, that I didn&#8217;t dive too deep into Java&#8217;s Serializable interface and more or less just followed Eclipse&#8217;s quick fixes.<\/p>\n<p><strong>To be more specific to my implementation:<\/strong><\/p>\n<p>I have a class that should send an instance of itself via a ObjectOutputStream.<\/p>\n<p>Is this a fundamentally wrong approach?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I override a createSocket() method in my test cases to pas in a mocked Socket. After doing this the objects aren&#8217;t serializable anymore. Here&#8217;s a example of what doesn&#8217;t work. Foo.java import java.io.Serializable; public class Foo implements Serializable { private static final long serialVersionUID = 3109852436898487119L; public void bar() { System.out.println(&#8220;Foo&#8221;); } } FooTest.java import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5327","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5327"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5327\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}