考虑一个方法签名,如:
public String myFunction(String abc);
Run Code Online (Sandbox Code Playgroud)
Mockito可以帮助返回方法收到的相同字符串吗?
Ste*_*eve 933
您可以在Mockito中创建答案.让我们假设,我们有一个名为Application的接口,其方法为myFunction.
public interface Application {
public String myFunction(String abc);
}
Run Code Online (Sandbox Code Playgroud)
以下是Mockito答案的测试方法:
public void testMyFunction() throws Exception {
Application mock = mock(Application.class);
when(mock.myFunction(anyString())).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
return (String) args[0];
}
});
assertEquals("someString",mock.myFunction("someString"));
assertEquals("anotherString",mock.myFunction("anotherString"));
}
Run Code Online (Sandbox Code Playgroud)
从Mockito 1.9.5和Java 8开始,使用lambda函数有一种更简单的方法:
when(myMock.myFunction(anyString())).thenAnswer(i -> i.getArguments()[0]);
Daw*_*ica 534
如果你有Mockito 1.9.5或更高版本,有一个新的静态方法可以Answer为你创建对象.你需要写一些类似的东西
import static org.mockito.Mockito.when;
import static org.mockito.AdditionalAnswers.returnsFirstArg;
when(myMock.myFunction(anyString())).then(returnsFirstArg());
Run Code Online (Sandbox Code Playgroud)
或者
doAnswer(returnsFirstArg()).when(myMock).myFunction(anyString());
Run Code Online (Sandbox Code Playgroud)
请注意,该returnsFirstArg()方法在AdditionalAnswers类中是静态的,这是Mockito 1.9.5的新增内容; 所以你需要正确的静态导入.
Paw*_*yda 66
使用Java 8,即使使用较旧版本的Mockito,也可以创建一个单行答案:
when(myMock.myFunction(anyString()).then(i -> i.getArgumentAt(0, String.class));
Run Code Online (Sandbox Code Playgroud)
当然,这并不像AdditionalAnswersDavid Wallace建议的那样有用,但是如果你想"动态地"转换参数可能会有用.
mig*_*igu 38
我有一个非常相似的问题.目标是模拟一个持久化对象的服务,并可以按名称返回它们.该服务看起来像这样:
public class RoomService {
public Room findByName(String roomName) {...}
public void persist(Room room) {...}
}
Run Code Online (Sandbox Code Playgroud)
服务模拟使用映射来存储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;
}
});
Run Code Online (Sandbox Code Playgroud)
我们现在可以在这个模拟上运行我们的测试.例如:
String name = "room";
Room room = new Room(name);
roomService.persist(room);
assertThat(roomService.findByName(name), equalTo(room));
assertNull(roomService.findByName("none"));
Run Code Online (Sandbox Code Playgroud)
yiw*_*wei 31
使用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"));
}
Run Code Online (Sandbox Code Playgroud)
编辑:更短:
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"));
}
Run Code Online (Sandbox Code Playgroud)
这有点旧,但我来到这里是因为我遇到了同样的问题。我正在使用 JUnit,但这次是在带有 mockk 的 Kotlin 应用程序中。我在这里发布一个示例以供参考并与 Java 副本进行比较:
@Test
fun demo() {
// mock a sample function
val aMock: (String) -> (String) = mockk()
// make it return the same as the argument on every invocation
every {
aMock.invoke(any())
} answers {
firstArg()
}
// test it
assertEquals("senko", aMock.invoke("senko"))
assertEquals("senko1", aMock.invoke("senko1"))
assertNotEquals("not a senko", aMock.invoke("senko"))
}
Run Code Online (Sandbox Code Playgroud)
我使用类似的东西(基本上它是相同的方法).有时,让模拟对象返回某些输入的预定义输出是有用的.这是这样的:
private Hashtable<InputObject, OutputObject> table = new Hashtable<InputObject, OutputObject>();
table.put(input1, ouput1);
table.put(input2, ouput2);
...
when(mockObject.method(any(InputObject.class))).thenAnswer(
new Answer<OutputObject>()
{
@Override
public OutputObject answer(final InvocationOnMock invocation) throws Throwable
{
InputObject input = (InputObject) invocation.getArguments()[0];
if (table.containsKey(input))
{
return table.get(input);
}
else
{
return null; // alternatively, you could throw an exception
}
}
}
);
Run Code Online (Sandbox Code Playgroud)
您可能希望将 verify() 与 ArgumentCaptor 结合使用以确保在测试中执行,并使用 ArgumentCaptor 来评估参数:
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
verify(mock).myFunction(argument.capture());
assertEquals("the expected value here", argument.getValue());
Run Code Online (Sandbox Code Playgroud)
显然可以通过argument.getValue() 访问参数的值,以便进一步操作/检查/无论如何。
这是一个很老的问题,但我认为仍然很重要。同样,可接受的答案仅适用于String。同时,有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());
Run Code Online (Sandbox Code Playgroud)
myClass.myFunction看起来像:
public class MyClass {
public ClassOfArgument myFunction(ClassOfArgument argument){
return argument;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过使用ArgumentCaptor来实现这一点
想象一下你有这样的 bean 函数。
public interface Application {
public String myFunction(String abc);
}
Run Code Online (Sandbox Code Playgroud)
然后在您的测试类中:
//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.
}
});
Run Code Online (Sandbox Code Playgroud)
或者,如果您喜欢 lambda,只需执行以下操作:
//Use ArgumentCaptor to capture the value
ArgumentCaptor<String> param = ArgumentCaptor.forClass(String.class);
when(mock.myFunction(param.capture()))
.thenAnswer((invocation) -> param.getValue());
Run Code Online (Sandbox Code Playgroud)
总结:使用argumentcaptor,捕获传入的参数。稍后在回答中返回使用 getValue 捕获的值。
| 归档时间: |
|
| 查看次数: |
318332 次 |
| 最近记录: |