[英]how to change an object that is passed by reference to a mock in Mockito
给出以下代码
@Mock
Client client;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
client.retrieveFile(baos); // client is supposed to fill boas with data
我如何指示Mockito填充baos
对象?
你可以使用Mockitos 答案 。
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
ByteArrayOutputStream baos = (ByteArrayOutputStream)args[0];
//fill baos with data
return null;
}
}).when(client).retrieveFile(baos);
但是,如果您有可能重构测试的代码,最好是让客户端返回OutputStream或可以放入此Output流的一些数据。 这将是更好的设计。
尝试这个
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
ByteArrayOutputStream baos = (ByteArrayOutputStream) invocation.getArguments()[0];
// fill it here
return null;
}}).when(client).retrieveFile(baos);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.