4

我有一个 mina 客户端,其连接器是 NioSocketConnector。我已经编写了这个客户端与真实服务器的集成测试。但是,我找不到进行单元测试的方法。例如,我想在不打开真实套接字的情况下测试我的自定义解码器和编码器是否正常工作。而且,我想测试我的消息在缓冲区等中是否正确排队。

我找到了一个用于测试的 DummySession 类,但我不确定这个类是否足以对客户端进行完整的单元测试。

Mina 建议使用它进行单元测试很容易,所以我真的想知道我该怎么做。请提供您的想法或示例代码链接。

提前致谢。

4

1 回答 1

8

编码器的单元测试:

YourEncoder encoder = new YourEncoder();
ProtocolCodecSession session = new ProtocolCodecSession();
encoder.encode(session, message, session.getEncoderOutput());
// encoded message will be in session.getEncoderOutputQueue()

解码器的单元测试:

//Prepare an IoBuffer which will be decoded, say buf
YourDecoder decoder = new YourDecoder();
ProtocolCodecSession session = new ProtocolCodecSession();
decoder.decode(session, buf, session.getDecoderOutput());
//decoded message will be in session.getDecoderOutputQueue()
于 2012-10-14T14:37:51.697 回答