Java Mockito:如何模拟在另一个方法中调用的方法

Java Mockito:如何模拟在另一个方法中调用的方法,java,junit,mockito,Java,Junit,Mockito,我是mockito的新手,我正在使用mockito测试调用另一个方法的方法,被调用的方法返回字符串。 我试过了,但我不能写测试。请帮忙 public class MyClass { protected String processIncominData(String input) { String request = ...; ... String response = forwardRequest(request); ... return respo

我是mockito的新手,我正在使用mockito测试调用另一个方法的方法,被调用的方法返回字符串。 我试过了,但我不能写测试。请帮忙

public class MyClass {
  protected String processIncominData(String input) {
    String request = ...;
    ...
    String response = forwardRequest(request);
    ...
    return response;
  }

  public String forwardRequest(String requestToSocket) {
   String hostname = socketServerName;
    int port = socketServerPort;
    String responseLine=null;
    Socket clientSocket = null;  
    PrintStream outs=null;

    BufferedReader is = null;
    BufferedWriter bwriter=null;

    try {
        clientSocket = new Socket(hostname, port);
        outs=new PrintStream(clientSocket.getOutputStream());
        is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        bwriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

    } catch (UnknownHostException e) {
        LOGGER.error("Don't know about host: " + hostname + e.getMessage());
    } catch (IOException e) {
        LOGGER.error("Couldn't get I/O for the connection to: " + hostname + e.getMessage());
    }

    if (clientSocket == null || outs == null || is == null) {
        LOGGER.error("Something is wrong. One variable is null.");
    }
    try {
        while ( true ) {
            StringBuilder sb = new StringBuilder();
            sb.append(requestToSocket);

            String  request = sb.toString().trim();
            bwriter.write(request);
            bwriter.write("\r\n");
            bwriter.flush();
            responseLine = is.readLine();
            LOGGER.info("Socket returns : " + responseLine);
            //outs.println(responseLine);
            bwriter.close();
        }
    } catch (UnknownHostException e) {
        LOGGER.error("Trying to connect to unknown host: "+ e.getMessage());
    } catch (IOException e) {
        LOGGER.error("IOException:  "+ e.getMessage());
    }
    finally{
        try {
            outs.close();
            is.close();
            clientSocket.close(); 
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return responseLine;
  }
}

我想在这里模拟xml响应以测试processIncomingData方法。此响应来自套接字服务器,我正在通过套接字客户端发送请求。我认为,若我可以模拟来自套接字的xmlResponse,那个么套接字将无关紧要。请给出任何有用的答案

您可以尝试
spy
方法:

MyClass myClass = spy(new MyClass());
when(myClass.forwardRequest("foo")).thenReturn("bar");
那么当你打电话的时候

myClass.processIncominData("baz");
响应将为“bar”(如果请求为“foo”)


PS:当您需要模拟测试中的类时,它会显示您的设计存在一些问题。

现在您已经发布了它,如何模拟它的答案在这里


但最好重构它,使其在不同的类中有数千个方法。通常认为模拟被测类是不好的做法。@JohnB是的。是wy,我在单独的课堂上添加了评论。我不知道这是你自己帖子上的评论。最好是编辑你的帖子,将扩展内容放在正文中。@JohnB这不是答案的一部分,只是一般性的提示。但是我编辑了我的答案。显示
xmlResponse
是如何生成的。您需要在
stringxmlresponse=…]行使用一个mock。如果这是一个实例方法调用,那么使用
Mockito
来模拟bean(读取socketclient)。如果这是一个静态调用,请使用
JMockit
模拟静态调用。它是字符串格式,如“123”等。您的单元测试不应该依赖于套接字服务器。你应该模拟客户端。你能告诉我如何模拟客户端吗?发布更多代码。具体来说,1)检索
xmlResponse
2)如何
MyClass
获取它正在使用的客户端实例的完整代码行。但我在第一个方法中没有使用套接字?