java mockito respose_java – Mockito:如何在另一个方法中调用mock方法

我是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方法..这个响应来自套接字服务器,我正在通过套接字客户端发送请求.我认为如果我可以从socket模拟xmlResponse,套接字无关紧要.请给出任何有用的答案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值