【问题标题】:Making a mocked method return an argument that was passed to it使模拟方法返回传递给它的参数
【发布时间】:2010-04-21 16:11:41
【问题描述】:

考虑一个方法签名,如:

public String myFunction(String abc);

Mockito 能否帮助返回与方法接收到的字符串相同的字符串?

【问题讨论】:

  • 好吧,一般来说,任何 java 模拟框架怎么样...这可以与任何其他框架一起使用,还是我应该创建一个哑存根来模仿我想要的行为?

标签: java mockito


【解决方案1】:

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

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

这是带有 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"));
}

从 Mockito 1.9.5 和 Java 8 开始,您还可以使用 lambda 表达式:

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

【讨论】:

  • 这也是我一直在寻找的。谢谢!不过,我的问题有所不同。我想模拟一个存储对象并按名称返回它们的持久性服务 (EJB)。
  • 我创建了一个额外的类来包装答案的创建。所以代码看起来像when(...).then(Return.firstParameter())
  • 使用 Java 8 lambdas 很容易返回第一个参数,即使对于特定的类,即when(foo(any()).then(i -&gt; i.getArgumentAt(0, Bar.class))。您也可以使用方法引用并调用真实方法。
  • 这解决了我的问题,方法是返回 Iterator&lt;? extends ClassName&gt;,这会导致 thenReturn() 语句中出现各种类型的转换问题。
  • 使用 Java 8 和 Mockito when(foo(any()).thenAnswer(i -> i.getArguments()[0])
【解决方案2】:

如果你有 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 的新功能;所以你需要正确的静态导入。

【讨论】:

  • 注意:它是when(...).then(returnsFirstArg()),我错误地把when(...).thenReturn(returnsFirstArg()) 给了java.lang.ClassCastException: org.mockito.internal.stubbing.answers.ReturnsArgumentAt cannot be cast to
  • 注意:returnsFirstArg() 返回的是 Answer 而不是参数的值。尝试调用 .thenReturn(new Foo(returnsFirstArg()))
  • 在过去的几年里,我总是需要一次又一次地用谷歌搜索这个答案,因为我只是不记得“AdditionalAnswers”而且我很少需要它。然后我想知道我怎么能建立那个场景,因为我找不到必要的依赖项。这不能直接添加到mockito吗? ://
  • 史蒂夫的回答更笼统。这仅允许您返回原始参数。如果您想处理该参数并返回结果,那么史蒂夫的回答规则。我都赞成,因为它们都很有用。
  • 仅供参考,我们必须导入static org.mockito.AdditionalAnswers.returnsFirstArg。这使用returnsFirstArg。另外,我可以在 Mockito 2.20 中执行 when(myMock.myFunction(any())).then(returnsFirstArg())。*
【解决方案3】:

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

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

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

【讨论】:

  • 太棒了。谢谢你。如果参数是long,这仍然可以与拳击和Long.class 一起使用吗?
  • .getArgumentAt(..) 没有为我找到,但 .getArgument(1) 有效(mockito 2.6.2)
【解决方案4】:

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

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

【讨论】:

    【解决方案5】:

    使用 Java 8,Steve's answer 可以变成

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

    【讨论】:

    • 这很酷,但它不适用于 thenThrow,不幸的是(thenThrow 不接受 InvocationOnMock 参数)。
    【解决方案6】:

    这是一个相当古老的问题,但我认为仍然相关。此外,接受的答案仅适用于字符串。同时有 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;
        }  
    }
    

    【讨论】:

      【解决方案7】:

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

      【讨论】:

        【解决方案8】:

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

        【讨论】:

        • 这不起作用(不再起作用了?)。关于文档:此方法必须在验证中使用。这意味着您只能在使用验证方法时捕获值
        • 1.不知道This doesn´t work (anymore?). 是什么意思,我在我的实例上工作。 2. 抱歉,我不清楚您要表达的意思。答案是针对 OP 的问题的。
        【解决方案9】:

        您可能希望将 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。

        【讨论】:

          【解决方案10】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-07-24
            • 1970-01-01
            • 2018-08-02
            • 1970-01-01
            • 2016-10-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多