diff --git a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/stub/StubInvocationUtilTest.java b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/stub/StubInvocationUtilTest.java index 56a192c319fb..9db612a3ef18 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/stub/StubInvocationUtilTest.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/stub/StubInvocationUtilTest.java @@ -28,6 +28,7 @@ import org.apache.dubbo.rpc.protocol.tri.support.IGreeter; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -40,110 +41,83 @@ import static org.mockito.Mockito.when; class StubInvocationUtilTest { + private Invoker invoker; + private MethodDescriptor method; + private String request = "request"; + private String response = "response"; + private Result result; - @Test - void unaryCall() throws Throwable { + private Invoker createMockInvoker(URL url) { Invoker invoker = Mockito.mock(Invoker.class); + when(invoker.getUrl()).thenReturn(url); + when(invoker.getInterface()).thenReturn(IGreeter.class); + return invoker; + } + + private URL createMockURL(ConsumerModel consumerModel) { URL url = Mockito.mock(URL.class); + when(url.getServiceModel()).thenReturn(consumerModel); + when(url.getServiceInterface()).thenReturn(IGreeter.class.getName()); + when(url.getProtocolServiceKey()).thenReturn(IGreeter.class.getName()); + return url; + } + + private ConsumerModel createMockConsumerModel(ServiceDescriptor serviceDescriptor) { ConsumerModel consumerModel = Mockito.mock(ConsumerModel.class); - ServiceDescriptor serviceDescriptor = Mockito.mock(ServiceDescriptor.class); when(consumerModel.getServiceModel()).thenReturn(serviceDescriptor); - when(url.getServiceModel()) - .thenReturn(consumerModel); - when(url.getServiceInterface()) - .thenReturn(IGreeter.class.getName()); - when(url.getProtocolServiceKey()) - .thenReturn(IGreeter.class.getName()); - when(invoker.getUrl()) - .thenReturn(url); - when(invoker.getInterface()) - .thenReturn(IGreeter.class); + return consumerModel; + } + + private Result createMockResult(String response) throws Throwable { Result result = Mockito.mock(Result.class); - when(invoker.invoke(any(Invocation.class))) - .thenReturn(result); - String response = "response"; when(result.recreate()).thenReturn(response); + return result; + } + + private MethodDescriptor createMockMethodDescriptor() { MethodDescriptor method = Mockito.mock(MethodDescriptor.class); - when(method.getParameterClasses()) - .thenReturn(new Class[]{String.class}); - when(method.getMethodName()) - .thenReturn("sayHello"); - String request = "request"; + when(method.getParameterClasses()).thenReturn(new Class[]{String.class}); + when(method.getMethodName()).thenReturn("sayHello"); + return method; + } + + @BeforeEach + void init() throws Throwable { + ServiceDescriptor serviceDescriptor = Mockito.mock(ServiceDescriptor.class); + ConsumerModel consumerModel = createMockConsumerModel(serviceDescriptor); + URL url = createMockURL(consumerModel); + invoker = createMockInvoker(url); + result = createMockResult("response"); + method = createMockMethodDescriptor(); + } + + @Test + void unaryCall() { + when(invoker.invoke(any(Invocation.class))).thenReturn(result); Object ret = StubInvocationUtil.unaryCall(invoker, method, request); Assertions.assertEquals(response, ret); } @Test - void unaryCall2() throws Throwable { - Invoker invoker = Mockito.mock(Invoker.class); - URL url = Mockito.mock(URL.class); - ConsumerModel consumerModel = Mockito.mock(ConsumerModel.class); - ServiceDescriptor serviceDescriptor = Mockito.mock(ServiceDescriptor.class); - when(consumerModel.getServiceModel()).thenReturn(serviceDescriptor); - when(url.getServiceModel()) - .thenReturn(consumerModel); - when(url.getServiceInterface()) - .thenReturn(IGreeter.class.getName()); - when(url.getProtocolServiceKey()) - .thenReturn(IGreeter.class.getName()); - when(invoker.getUrl()) - .thenReturn(url); - when(invoker.getInterface()) - .thenReturn(IGreeter.class); - Result result = Mockito.mock(Result.class); - when(invoker.invoke(any(Invocation.class))) - .thenThrow(new RuntimeException("a")) - .thenThrow(new Error("b")); - String response = "response"; - when(result.recreate()).thenReturn(response); - MethodDescriptor method = Mockito.mock(MethodDescriptor.class); - when(method.getParameterClasses()) - .thenReturn(new Class[]{String.class}); - when(method.getMethodName()) - .thenReturn("sayHello"); - String request = "request"; + void unaryCall2(){ + when(invoker.invoke(any(Invocation.class))).thenThrow(new RuntimeException("a")).thenThrow(new Error("b")); try { StubInvocationUtil.unaryCall(invoker, method, request); fail(); - }catch (Throwable t){ + } catch (Throwable t) { // pass } try { StubInvocationUtil.unaryCall(invoker, method, request); fail(); - }catch (Throwable t){ + } catch (Throwable t) { // pass } } @Test void testUnaryCall() throws Throwable { - Invoker invoker = Mockito.mock(Invoker.class); - URL url = Mockito.mock(URL.class); - ConsumerModel consumerModel = Mockito.mock(ConsumerModel.class); - ServiceDescriptor serviceDescriptor = Mockito.mock(ServiceDescriptor.class); - when(consumerModel.getServiceModel()).thenReturn(serviceDescriptor); - when(url.getServiceModel()) - .thenReturn(consumerModel); - when(url.getServiceInterface()) - .thenReturn(IGreeter.class.getName()); - when(url.getProtocolServiceKey()) - .thenReturn(IGreeter.class.getName()); - when(invoker.getUrl()) - .thenReturn(url); - when(invoker.getInterface()) - .thenReturn(IGreeter.class); - Result result = Mockito.mock(Result.class); - String response = "response"; - when(invoker.invoke(any(Invocation.class))) - .then(invocationOnMock -> result); - when(result.recreate()).thenReturn(response); - MethodDescriptor method = Mockito.mock(MethodDescriptor.class); - when(method.getParameterClasses()) - .thenReturn(new Class[]{String.class}); - when(method.getMethodName()) - .thenReturn("sayHello"); - String request = "request"; + when(invoker.invoke(any(Invocation.class))).then(invocationOnMock -> result); CountDownLatch latch = new CountDownLatch(1); AtomicReference atomicReference = new AtomicReference<>(); StreamObserver responseObserver = new StreamObserver() { @@ -168,54 +142,29 @@ public void onCompleted() { @Test void biOrClientStreamCall() throws InterruptedException { - Invoker invoker = Mockito.mock(Invoker.class); - URL url = Mockito.mock(URL.class); - ConsumerModel consumerModel = Mockito.mock(ConsumerModel.class); - ServiceDescriptor serviceDescriptor = Mockito.mock(ServiceDescriptor.class); - when(consumerModel.getServiceModel()).thenReturn(serviceDescriptor); - when(url.getServiceModel()) - .thenReturn(consumerModel); - when(url.getServiceInterface()) - .thenReturn(IGreeter.class.getName()); - when(url.getProtocolServiceKey()) - .thenReturn(IGreeter.class.getName()); - when(invoker.getUrl()) - .thenReturn(url); - when(invoker.getInterface()) - .thenReturn(IGreeter.class); - Result result = Mockito.mock(Result.class); - String response = "response"; - - when(invoker.invoke(any(Invocation.class))) - .then(invocationOnMock -> { - Invocation invocation = (Invocation) invocationOnMock.getArguments()[0]; - StreamObserver observer = (StreamObserver) invocation.getArguments()[0]; - observer.onNext(response); - observer.onCompleted(); - when(result.recreate()).then(invocationOnMock1 -> new StreamObserver() { - @Override - public void onNext(Object data) { - observer.onNext(data); - } + when(invoker.invoke(any(Invocation.class))).then(invocationOnMock -> { + Invocation invocation = (Invocation) invocationOnMock.getArguments()[0]; + StreamObserver observer = (StreamObserver) invocation.getArguments()[0]; + observer.onNext(response); + observer.onCompleted(); + when(result.recreate()).then(invocationOnMock1 -> new StreamObserver() { + @Override + public void onNext(Object data) { + observer.onNext(data); + } - @Override - public void onError(Throwable throwable) { + @Override + public void onError(Throwable throwable) { - } + } - @Override - public void onCompleted() { - observer.onCompleted(); - } - }); - return result; + @Override + public void onCompleted() { + observer.onCompleted(); + } }); - MethodDescriptor method = Mockito.mock(MethodDescriptor.class); - when(method.getParameterClasses()) - .thenReturn(new Class[]{String.class}); - when(method.getMethodName()) - .thenReturn("sayHello"); - String request = "request"; + return result; + }); CountDownLatch latch = new CountDownLatch(11); StreamObserver responseObserver = new StreamObserver() { @Override @@ -232,8 +181,7 @@ public void onCompleted() { latch.countDown(); } }; - StreamObserver observer = StubInvocationUtil.biOrClientStreamCall(invoker, method, - responseObserver); + StreamObserver observer = StubInvocationUtil.biOrClientStreamCall(invoker, method, responseObserver); for (int i = 0; i < 10; i++) { observer.onNext(request); } @@ -243,39 +191,15 @@ public void onCompleted() { @Test void serverStreamCall() throws InterruptedException { - Invoker invoker = Mockito.mock(Invoker.class); - URL url = Mockito.mock(URL.class); - ConsumerModel consumerModel = Mockito.mock(ConsumerModel.class); - ServiceDescriptor serviceDescriptor = Mockito.mock(ServiceDescriptor.class); - when(consumerModel.getServiceModel()).thenReturn(serviceDescriptor); - when(url.getServiceModel()) - .thenReturn(consumerModel); - when(url.getServiceInterface()) - .thenReturn(IGreeter.class.getName()); - when(url.getProtocolServiceKey()) - .thenReturn(IGreeter.class.getName()); - when(invoker.getUrl()) - .thenReturn(url); - when(invoker.getInterface()) - .thenReturn(IGreeter.class); - Result result = Mockito.mock(Result.class); - String response = "response"; - when(invoker.invoke(any(Invocation.class))) - .then(invocationOnMock -> { - Invocation invocation = (Invocation) invocationOnMock.getArguments()[0]; - StreamObserver observer = (StreamObserver) invocation.getArguments()[1]; - for (int i = 0; i < 10; i++) { - observer.onNext(response); - } - observer.onCompleted(); - return result; - }); - MethodDescriptor method = Mockito.mock(MethodDescriptor.class); - when(method.getParameterClasses()) - .thenReturn(new Class[]{String.class}); - when(method.getMethodName()) - .thenReturn("sayHello"); - String request = "request"; + when(invoker.invoke(any(Invocation.class))).then(invocationOnMock -> { + Invocation invocation = (Invocation) invocationOnMock.getArguments()[0]; + StreamObserver observer = (StreamObserver) invocation.getArguments()[1]; + for (int i = 0; i < 10; i++) { + observer.onNext(response); + } + observer.onCompleted(); + return result; + }); CountDownLatch latch = new CountDownLatch(11); StreamObserver responseObserver = new StreamObserver() { @Override