考虑如下方法签名:
public String myFunction(String abc);
Mockito可以帮助返回与方法接收到的字符串相同的字符串吗?
考虑如下方法签名:
public String myFunction(String abc);
Mockito可以帮助返回与方法接收到的字符串相同的字符串吗?
当前回答
使用Java 8,Steve的答案可以变成
public void testMyFunction() throws Exception {
Application mock = mock(Application.class);
when(mock.myFunction(anyString())).thenAnswer(
invocation -> {
Object[] args = invocation.getArguments();
return args[0];
});
assertEquals("someString", mock.myFunction("someString"));
assertEquals("anotherString", mock.myFunction("anotherString"));
}
编辑:更短:
public void testMyFunction() throws Exception {
Application mock = mock(Application.class);
when(mock.myFunction(anyString())).thenAnswer(
invocation -> invocation.getArgument(0));
assertEquals("someString", mock.myFunction("someString"));
assertEquals("anotherString", mock.myFunction("anotherString"));
}
其他回答
如果您有Mockito 1.9.5或更高版本,有一种新的静态方法可以为您创建Answer对象。你需要写一些像
import static org.mockito.Mockito.when;
import static org.mockito.AdditionalAnswers.returnsFirstArg;
when(myMock.myFunction(anyString())).then(returnsFirstArg());
或替代地
doAnswer(returnsFirstArg()).when(myMock).myFunction(anyString());
注意,returnsFirstArg()方法在AdditionalAnswers类中是静态的,这是Mockito 1.9.5的新特性;所以您需要正确的静态导入。
这是一个很老的问题,但我认为仍然相关。此外,接受的答案仅适用于字符串。同时还有Mockito 2.1,一些进口已经改变,所以我想分享我目前的答案:
import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@Mock
private MyClass myClass;
// this will return anything you pass, but it's pretty unrealistic
when(myClass.myFunction(any())).then(returnsFirstArg());
// it is more "life-like" to accept only the right type
when(myClass.myFunction(any(ClassOfArgument.class))).then(returnsFirstArg());
myClass.myFunction如下所示:
public class MyClass {
public ClassOfArgument myFunction(ClassOfArgument argument){
return argument;
}
}
我有一个非常类似的问题。目标是模拟一个持久化对象并可以按名称返回对象的服务。服务如下所示:
public class RoomService {
public Room findByName(String roomName) {...}
public void persist(Room room) {...}
}
服务模拟使用映射来存储Room实例。
RoomService roomService = mock(RoomService.class);
final Map<String, Room> roomMap = new HashMap<String, Room>();
// mock for method persist
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
if (arguments != null && arguments.length > 0 && arguments[0] != null) {
Room room = (Room) arguments[0];
roomMap.put(room.getName(), room);
}
return null;
}
}).when(roomService).persist(any(Room.class));
// mock for method findByName
when(roomService.findByName(anyString())).thenAnswer(new Answer<Room>() {
@Override
public Room answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
if (arguments != null && arguments.length > 0 && arguments[0] != null) {
String key = (String) arguments[0];
if (roomMap.containsKey(key)) {
return roomMap.get(key);
}
}
return null;
}
});
我们现在可以在这个模拟上运行测试了。例如:
String name = "room";
Room room = new Room(name);
roomService.persist(room);
assertThat(roomService.findByName(name), equalTo(room));
assertNull(roomService.findByName("none"));
使用Java 8,Steve的答案可以变成
public void testMyFunction() throws Exception {
Application mock = mock(Application.class);
when(mock.myFunction(anyString())).thenAnswer(
invocation -> {
Object[] args = invocation.getArguments();
return args[0];
});
assertEquals("someString", mock.myFunction("someString"));
assertEquals("anotherString", mock.myFunction("anotherString"));
}
编辑:更短:
public void testMyFunction() throws Exception {
Application mock = mock(Application.class);
when(mock.myFunction(anyString())).thenAnswer(
invocation -> invocation.getArgument(0));
assertEquals("someString", mock.myFunction("someString"));
assertEquals("anotherString", mock.myFunction("anotherString"));
}
您可以通过使用ArgumentCaptor实现这一点
假设您有这样的bean函数。
public interface Application {
public String myFunction(String abc);
}
然后在测试课中:
//Use ArgumentCaptor to capture the value
ArgumentCaptor<String> param = ArgumentCaptor.forClass(String.class);
when(mock.myFunction(param.capture())).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return param.getValue();//return the captured value.
}
});
或者,如果你喜欢lambda,只需:
//Use ArgumentCaptor to capture the value
ArgumentCaptor<String> param = ArgumentCaptor.forClass(String.class);
when(mock.myFunction(param.capture()))
.thenAnswer((invocation) -> param.getValue());
摘要:使用argumentcaptor捕获传递的参数。稍后在回答中返回使用getValue捕获的值。