diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/EchoFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/EchoFilterTest.java index 2c67db6ffc..3408b2ebc5 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/EchoFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/EchoFilterTest.java @@ -38,20 +38,9 @@ class EchoFilterTest { @SuppressWarnings("unchecked") @Test void testEcho() { - Invocation invocation = mock(RpcInvocation.class); + Invocation invocation = createMockRpcInvocation(); + Invoker invoker = createMockInvoker(invocation); given(invocation.getMethodName()).willReturn("$echo"); - given(invocation.getParameterTypes()).willReturn(new Class[]{Enum.class}); - given(invocation.getArguments()).willReturn(new Object[]{"hello"}); - given(invocation.getObjectAttachments()).willReturn(null); - - Invoker invoker = mock(Invoker.class); - given(invoker.isAvailable()).willReturn(true); - given(invoker.getInterface()).willReturn(DemoService.class); - AppResponse result = new AppResponse(); - result.setValue("High"); - given(invoker.invoke(invocation)).willReturn(result); - URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1"); - given(invoker.getUrl()).willReturn(url); Result filterResult = echoFilter.invoke(invoker, invocation); assertEquals("hello", filterResult.getValue()); @@ -60,22 +49,31 @@ class EchoFilterTest { @SuppressWarnings("unchecked") @Test void testNonEcho() { - Invocation invocation = mock(Invocation.class); + Invocation invocation = createMockRpcInvocation(); + Invoker invoker = createMockInvoker(invocation); given(invocation.getMethodName()).willReturn("echo"); + + Result filterResult = echoFilter.invoke(invoker, invocation); + assertEquals("High", filterResult.getValue()); + } + + Invocation createMockRpcInvocation() { + Invocation invocation = mock(RpcInvocation.class); given(invocation.getParameterTypes()).willReturn(new Class[]{Enum.class}); given(invocation.getArguments()).willReturn(new Object[]{"hello"}); given(invocation.getObjectAttachments()).willReturn(null); - + return invocation; + } + Invoker createMockInvoker(Invocation invocation){ Invoker invoker = mock(Invoker.class); given(invoker.isAvailable()).willReturn(true); given(invoker.getInterface()).willReturn(DemoService.class); + AppResponse result = new AppResponse(); result.setValue("High"); given(invoker.invoke(invocation)).willReturn(result); URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1"); given(invoker.getUrl()).willReturn(url); - - Result filterResult = echoFilter.invoke(invoker, invocation); - assertEquals("High", filterResult.getValue()); + return invoker; } }