### Question:
SelectTelnetHandler implements TelnetHandler { @Override @SuppressWarnings("unchecked") public String telnet(Channel channel, String message) { if (message == null || message.length() == 0) { return "Please input the index of the method you want to invoke, eg: \r\n select 1"; } List<Method> methodList = (List<Method>) channel.getAttribute(InvokeTelnetHandler.INVOKE_METHOD_LIST_KEY); if (CollectionUtils.isEmpty(methodList)) { return "Please use the invoke command first."; } if (!StringUtils.isInteger(message) || Integer.parseInt(message) < 1 || Integer.parseInt(message) > methodList.size()) { return "Illegal index ,please input select 1~" + methodList.size(); } Method method = methodList.get(Integer.parseInt(message)); channel.setAttribute(SELECT_METHOD_KEY, method); channel.setAttribute(SELECT_KEY, Boolean.TRUE); String invokeMessage = (String) channel.getAttribute(InvokeTelnetHandler.INVOKE_MESSAGE_KEY); return invokeTelnetHandler.telnet(channel, invokeMessage); }  @Override @SuppressWarnings("unchecked") String telnet(Channel channel, String message); static final String SELECT_METHOD_KEY; static final String SELECT_KEY; }### Answer:
@Test public void testInvokeWithoutMethodList() throws RemotingException { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(DemoService.class.getName()); given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String result = select.telnet(mockChannel, "1"); assertTrue(result.contains("Please use the invoke command first.")); }
@Test public void testInvokeWithIllegalMessage() throws RemotingException { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(DemoService.class.getName()); given(mockChannel.getAttribute(InvokeTelnetHandler.INVOKE_METHOD_LIST_KEY)).willReturn(methods); given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String result = select.telnet(mockChannel, "index"); assertTrue(result.contains("Illegal index ,please input select 1")); result = select.telnet(mockChannel, "0"); assertTrue(result.contains("Illegal index ,please input select 1")); result = select.telnet(mockChannel, "1000"); assertTrue(result.contains("Illegal index ,please input select 1")); }
@Test public void testInvokeWithNull() throws RemotingException { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(DemoService.class.getName()); given(mockChannel.getAttribute(InvokeTelnetHandler.INVOKE_METHOD_LIST_KEY)).willReturn(methods); given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String result = select.telnet(mockChannel, null); assertTrue(result.contains("Please input the index of the method you want to invoke")); }