简体   繁体   中英

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()  
        );
    }
}

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM