Menu

#2 Be able to serialize mocks

EasyMock_2.4
closed
EasyMock (33)
5
2012-10-05
2008-02-17
No

In some cases it would be useful to be able to serialize a mock.

In particular, to inject mocking code to a server.

Discussion

  • Chris Wilkes

    Chris Wilkes - 2008-02-24

    Logged In: YES
    user_id=352203
    Originator: NO

    For a quick fix have ObjectMethodsFilter implement Serializable. I also subclassed MocksControl implementing Serializable, not sure if that is necessary.

     
  • Henri Tremblay

    Henri Tremblay - 2008-02-24

    Logged In: YES
    user_id=893525
    Originator: YES

    I've started to implement it. Making ObjectMethodFilter serializable requires many objects underneath and custom serialization. I'm also trying to keep backward comptatibility with easymock class extension. Should be in check-in soon. However, depending on the impacts, I don't know when it will be officially released

     
  • Valerio Schiavoni

    Logged In: YES
    user_id=930308
    Originator: NO

    A test method, might be useful :
    @Test
    public void testSerializeMockInetAddress() throws IOException
    {
    InetAddress mockInetAddress = EasyMock.createMock(InetAddress.class);
    assertThat(mockInetAddress,instanceOf(Serializable.class));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(mockInetAddress);
    final byte[] itaSerialized = baos.toByteArray();
    assertThat(itaSerialized,is(not(nullValue())));
    }

    Currently this test fails with the following stacktrace:
    java.io.NotSerializableException: org.easymock.classextension.internal.ClassProxyFactory$1
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1081)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302)

    (this code was posted on the official easymock mailing-list, i thought it was useful to keep track of it on the official tracker of the project).

     
  • Henri Tremblay

    Henri Tremblay - 2008-07-05

    Logged In: YES
    user_id=893525
    Originator: YES

    Submitted by Jonathan Locked

     

Log in to post a comment.