【问题标题】:Mockito:How to mock method called inside another methodMockito:如何模拟在另一个方法中调用的方法
【发布时间】:2014-11-25 12:16:16
【问题描述】:

我是 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,我认为套接字无关紧要。请给出任何有用的答案

【问题讨论】:

  • 显示xmlResponse 是如何生成的。您需要在String xmlResponse = ...; 行使用模拟。如果这是一个实例方法调用,请使用Mockito 模拟 bean(读取套接字客户端)。如果这是一个静态调用,请使用JMockit 模拟静态调用。
  • 我正在从套接字服务器读取 xml 响应。它是字符串格式,如“123”等
  • 您的单元测试不应依赖于套接字服务器。你应该在嘲笑客户。
  • 你能告诉我如何模拟客户端吗?
  • 发布更多代码。具体来说,1) 检索 xmlResponse 的完整代码行 2) MyClass 如何获取它正在使用的客户端实例。

标签: java junit mockito


【解决方案1】:

既然你已经发布了它,如何模拟它的答案就在这里。

testing-java-sockets

【讨论】:

  • 但我的第一种方法中没有使用套接字?
【解决方案2】:

你可以试试spy方法:

MyClass myClass = spy(new MyClass());
when(myClass.forwardRequest("foo")).thenReturn("bar");

然后当你打电话时

myClass.processIncominData("baz");

响应将是“bar”(如果请求是“foo”)

PS:当您需要模拟被测类时,它表明您的设计存在一些问题。

【讨论】:

  • 但最好重构一下,让它在不同的类中有 thou 个方法。
  • 模拟被测类通常被认为是不好的做法。
  • @JohnB 是的。这就是为什么我要在单独的课程中添加评论。
  • 不知道这是对您自己帖子的评论。编辑您的帖子以将扩展放在正文中可能会更好。
  • @JohnB 这不是答案的一部分,只是一般提示。但我编辑了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
相关资源
最近更新 更多