|
33 | 33 |
|
34 | 34 | import static org.junit.Assert.assertEquals; |
35 | 35 | import static org.junit.Assert.assertTrue; |
| 36 | +import static org.junit.Assert.fail; |
36 | 37 | import static org.mockito.ArgumentMatchers.any; |
37 | 38 | import static org.mockito.BDDMockito.given; |
38 | 39 | import static org.mockito.Mockito.mock; |
@@ -68,6 +69,41 @@ public void testInvokeDefaultSService() throws RemotingException { |
68 | 69 | assertTrue(result.contains("Use default service com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService.\r\n\"ok\"\r\n")); |
69 | 70 | } |
70 | 71 |
|
| 72 | + @SuppressWarnings("unchecked") |
| 73 | + @Test |
| 74 | + public void testInvokeByPassingNullValue() throws RemotingException { |
| 75 | + mockInvoker = mock(Invoker.class); |
| 76 | + given(mockInvoker.getInterface()).willReturn(DemoService.class); |
| 77 | + given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:20883/demo")); |
| 78 | + given(mockInvoker.invoke(any(Invocation.class))).willReturn(new RpcResult("ok")); |
| 79 | + mockChannel = mock(Channel.class); |
| 80 | + given(mockChannel.getAttribute("telnet.service")).willReturn("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService"); |
| 81 | + given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); |
| 82 | + given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20883")); |
| 83 | + DubboProtocol.getDubboProtocol().export(mockInvoker); |
| 84 | + // pass null value to parameter of primitive type |
| 85 | + try { |
| 86 | + invoke.telnet(mockChannel, "DemoService.add(null, 2)"); |
| 87 | + fail("It should cause a NullPointerException by the above code."); |
| 88 | + } catch (NullPointerException ex) { |
| 89 | + String message = ex.getMessage(); |
| 90 | + assertEquals("The type of No.1 parameter is primitive(int), but the value passed is null.", message); |
| 91 | + } |
| 92 | + try { |
| 93 | + invoke.telnet(mockChannel, "DemoService.add(1, null)"); |
| 94 | + fail("It should cause a NullPointerException by the above code."); |
| 95 | + } catch (NullPointerException ex) { |
| 96 | + String message = ex.getMessage(); |
| 97 | + assertEquals("The type of No.2 parameter is primitive(long), but the value passed is null.", message); |
| 98 | + } |
| 99 | + // pass null value to parameter of object type |
| 100 | + try { |
| 101 | + invoke.telnet(mockChannel, "DemoService.sayHello(null)"); |
| 102 | + } catch (NullPointerException ex) { |
| 103 | + fail("It shouldn't cause a NullPointerException by the above code."); |
| 104 | + } |
| 105 | + } |
| 106 | + |
71 | 107 | @SuppressWarnings("unchecked") |
72 | 108 | @Test |
73 | 109 | public void testInvokeAutoFindMethod() throws RemotingException { |
|
0 commit comments