繁体   English   中英

使模拟方法返回传递给它的参数

[英]Making a mocked method return an argument that was passed to it

考虑一个方法签名,如:

public String myFunction(String abc);

Mockito 可以帮助返回方法接收到的相同字符串吗?

从 Mockito 1.9.5+ 和 Java 8+

您可以使用 lambda 表达式,例如:

when(myMock.myFunction(anyString())).thenAnswer(i -> i.getArguments()[0]);

其中iInvocationOnMock的一个实例。

对于旧版本

您可以在 Mockito 中创建答案。 假设,我们有一个名为 MyInterface 的接口和一个方法 myFunction。

public interface MyInterface {
    public String myFunction(String abc);
}

这是带有 Mockito 答案的测试方法:

public void testMyFunction() throws Exception {
    MyInterface mock = mock(MyInterface.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"));
}

如果你有 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());

请注意,在AdditionalAnswers类中, returnsFirstArg()方法是静态的,这是 Mockito 1.9.5 的新功能; 所以你需要正确的静态导入。

使用 Java 8,即使使用旧版本的 Mockito,也可以创建单行答案:

when(myMock.myFunction(anyString()).then(i -> i.getArgumentAt(0, String.class));

当然,这不如使用 David Wallace 建议的AdditionalAnswers有用,但如果您想“即时”转换参数,可能会很有用。

我有一个非常相似的问题。 目标是模拟一个持久化对象的服务,并可以通过它们的名称返回它们。 该服务如下所示:

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;
    }
});

我们现在可以在这个 mock 上运行我们的测试。 例如:

String name = "room";
Room room = new Room(name);
roomService.persist(room);
assertThat(roomService.findByName(name), equalTo(room));
assertNull(roomService.findByName("none"));

使用 Java 8,史蒂夫的答案可以变成

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 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;
    }  
}

您可以通过使用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 捕获的值。

这有点旧,但我来这里是因为我有同样的问题。 我正在使用 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"))
}

您可能希望将 verify() 与 ArgumentCaptor 结合使用以确保在测试中执行,并使用 ArgumentCaptor 来评估参数:

ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
verify(mock).myFunction(argument.capture());
assertEquals("the expected value here", argument.getValue());

参数的值显然可以通过 argument.getValue() 访问以进行进一步的操作/检查/whatever。

我使用类似的东西(基本上是相同的方法)。 有时让模拟对象为某些输入返回预定义的输出很有用。 事情是这样的:

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
               }
           }
       }
       );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM