2017-10-17 11 views
0

私は、SimpleChannelInboundHandlerを実装し、ChannelHandlerContextとFullHTTPRequestを取るChannelRead0関数をオーバーライドするAPI Handlerを持っています。 誰も私にこれを手伝ってもらえますか?SimpleChannelInboundHandler <fullHTTPRequest>のユニットテスト(ネッティー)

public class MyContentExtractionHandler extends SimpleChannelInboundHandler<FullHttpRequest> { 
    @Override 
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception { 
     int contentLenght = msg.content().capacity(); 
     byte[] content = new byte[contentLenght]; 
     msg.content().getBytes(0, content); 
     ctx.fireChannelRead(new String(content)); 
    } 
} 

私は定期的にDefaultFullHttpRequestを作成し、ChannelHandlerContextを模擬するためにmockitoを使用します。

答えて

0

は、このようになりますこれは、私は私のMyContentExtractionHandlerをテストしたいと仮定します。私のユニットテストは、次のようになります。

public class MyContentExtractionHandlerTest { 

    @Mock 
    ChannelHandlerContext mockCtx = BDDMockito.mock(ChannelHandlerContext.class); 

    MyContentExtractionHandler myContentExtractorHandler = new MyContentExtractionHandler(); 

    @Test 
    public void myTest() throws Exception { 
     String content = "MyContentHello"; 
     DefaultFullHttpRequest fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/my /uri", Unpooled.copiedBuffer(content.getBytes())); 
     myContentExtractorHandler.channelRead(mockCtx, fullHttpRequest); 
     BDDMockito.verify(mockCtx).fireChannelRead(content); //verify that fireChannelRead was called once with the expected result 
    } 
} 

ほとんどおそらく、あなたのSimpleChannelInboundHandlerが最終ハンドラになります。だから、fireChannelRead()をチェックする代わりに、メッセージを読んだ後にあなたが呼び出すメソッドをチェックしてください。

+0

ここではMyUrlExtractorHandlerが何をするでしょうか?私はnettyとmockitoにはとても新しいです。 –

+0

申し訳ありませんが、私の更新の回答を参照してください。もっと詳しいことはできないと思います... –

関連する問題