Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6123263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:59:41+00:00 2026-05-23T15:59:41+00:00

I override a createSocket() method in my test cases to pas in a mocked

  • 0

I override a createSocket() method in my test cases to pas in a mocked Socket. After doing this the objects aren’t serializable anymore.

Here’s a example of what doesn’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("Foo");
    }
}

FooTest.java

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import junit.framework.TestCase;

import org.junit.Test;

public class FooTest extends TestCase {

    // this passes
    @Test
    public void testFooIsSerializable() throws IOException {
        Foo foo = new Foo();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(baos);
        out.writeObject(foo);
    }

    // this throws a java.io.NotSerializableException
    @Test
    public void testFooIsStillSerializableAfterBarIsOverridden()
            throws IOException {

        // Eclipse gives me the warning "The serializable class  does not declare a static final serialVersionUID field of type long"
        // Adding it doesn't help
        Foo foo = new Foo() {
            @Override
            public void bar() {
                System.out.println("Bar");
            }
        };

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(baos);
        out.writeObject(foo);
    }
}

The stack trace when running FooTest with JUnit:

java.io.NotSerializableException: FooTest
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
    at FooTest.testFooIsStillSerializableAfterBarIsOverridden(FooTest.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

What is the problem with this? I have to admit, that I didn’t dive too deep into Java’s Serializable interface and more or less just followed Eclipse’s quick fixes.

To be more specific to my implementation:

I have a class that should send an instance of itself via a ObjectOutputStream.

Is this a fundamentally wrong approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:59:42+00:00Added an answer on May 23, 2026 at 3:59 pm

    The problem is you have an anonymous inner class, which by definition contains a (compiler-generated) reference to the instance of the outer class (FooTest) which created it. Since serialization by default includes all members of the object, the runtime tries to serialize the FooTest object too – and that is not serializable (as it was not meant to be). This is attested by the error message:

    java.io.NotSerializableException: FooTest
    

    Thus if you turn your anonymous class into an explicit static inner class, the problem should go away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to override malloc by doing this. #define malloc(X) my_malloc((X)) void* my_malloc(size_t
@Override protected void onStop() { super.onStop(); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(Test message) .setCancelable(true)
I would like to override one method onMouseUp:function(evt){ if(evt.target===this.domNode||!this._highlighted_option){ return; }else{ if(evt.target==this.previousButton){ this.onPage(-1); }else{
protected override Boolean IsValid(String propertyValue) { return !String.IsNullOrEmpty(propertyValue) && propertyValue.Trim().Length > 0; } This
We'd like to override DataGridView's default behavior when using a mouse wheel with this
I am trying to override the DataGridViewTextBoxCell's paint method in a derived class so
@Override @Async public void asyncExceptionTest() { int i=1/0; } How can I log this
I want to override the onAttach() http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity ) method of a ListFragment from the
I'm trying to override an overridden method (if that makes sense!) in C#. I
Is it possible to override a private method by using Reflection in .NET 3.5?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.