GP89
GP89

Reputation: 6730

Writing unit tests for Kryo serializers

I have a bunch of serialisation classes I've written and want to write unit tests for them, basically to test if an object is serialised and unserialised that the resulting object is considered equal. I'm not sure how to do the serialisation however. I'm using it with storm and that part seems to magically happen on its own so I'm not sure I know how.

There's Input and Output objects that read and write to the stream but I'm not sure how I would turn an output into an input.

Here's a basic serialiser as an example

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

public class BasicSerialiser extends Serializer<BasicObject> {

    @Override
    public void write (Kryo kryo, Output output, BasicObject obj) {
        super.write(kryo, output, messageSummary);
        output.writeString(obj.name);
        output.writeDouble(obj.timestamp);
        output.writeBoolean(obj.isOrange());
    }

    @Override
    public BasicObject read(Kryo kryo, Input input, Class<BasicObject> aClass) {
        return new BasicObject(
                input.readString(),  
                input.readDouble(), 
                input.readBoolean()  
        );
    }
}

Upvotes: 3

Views: 1220

Answers (1)

Tomer
Tomer

Reputation: 562

Try this: http://www.programcreek.com/java-api-examples/index.php?api=com.esotericsoftware.kryo.Kryo

It helped me.

It gives examples of testing object serialization.

You can make sure that kryo is using your serializer as one of the assertions using something like:

assertTrue(kryo.getSerializer(MyClass.class).equals(MySerializer.class));

Upvotes: 1

Related Questions