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

Java 使模拟方法返回传递给它的参数,java,mockito,Java,Mockito,考虑一个方法签名,如: public String myFunction(String abc); Mockito能否帮助返回方法接收到的相同字符串?您可以在Mockito中创建答案。让我们假设,我们有一个名为Application的接口,其中有一个方法myFunction public interface Application { public String myFunction(String abc); } 以下是带有Mockito答案的测试方法: public void test

考虑一个方法签名,如:

public String myFunction(String abc);

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

您可以在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"));
}
我使用类似的方法(基本上是相同的方法)。有时,让模拟对象为某些输入返回预定义的输出是很有用的。事情是这样的:

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
               }
           }
       }
       );
public class RoomService {
    public Room findByName(String roomName) {...}
    public void persist(Room room) {...}
}
private Hashtable=new Hashtable();
表.put(输入1,输出1);
表.put(输入2,输出2);
...
当(mockObject.method(any(InputObject.class)),然后回答(
新答案()
{
@凌驾
public OutputObject应答(最终调用锁调用)抛出可丢弃
{
InputObject输入=(InputObject)调用.getArguments()[0];
if(表格containsKey(输入))
{
返回表.get(输入);
}
其他的
{
return null;//或者,您可以抛出一个异常
}
}
}
);

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

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
               }
           }
       }
       );
public class RoomService {
    public Room findByName(String roomName) {...}
    public void persist(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;
    }
});

如果您有Mockito 1.9.5或更高版本,那么有一种新的静态方法可以为您生成
答案
对象。你需要写一些像这样的东西

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中新增的;因此,您需要正确的静态导入。

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

when(myMock.myFunction(anyString()).then(i -> i.getArgumentAt(0, String.class));
当然,这不如使用David Wallace建议的
附加答案有用,但如果您想“动态”转换参数,这可能会很有用。

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

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

ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
verify(mock).myFunction(argument.capture());
assertEquals("the expected value here", argument.getValue());
ArgumentCaptor参数=ArgumentCaptor.forClass(String.class);
验证(mock).myFunction(argument.capture());
assertEquals(“此处的预期值”,argument.getValue());

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

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

这有点旧,但我来这里是因为我有同样的问题。我正在使用JUnit,但这次是在Kotlin应用程序中使用mockk。我在这里发布了一个示例,以供参考,并与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"))
}

您可以通过使用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.
    }
  });
//使用ArgumentCaptor捕获值
ArgumentCaptor param=ArgumentCaptor.forClass(String.class);
when(mock.myFunction(param.capture()).thenAnswer(new-Answer()){
@凌驾
公共字符串应答(invocationMock调用)抛出Throwable{
return param.getValue();//返回捕获的值。
}
});
如果您喜欢lambda,只需执行以下操作:

//Use ArgumentCaptor to capture the value
ArgumentCaptor<String> param = ArgumentCaptor.forClass(String.class);


when(mock.myFunction(param.capture()))
    .thenAnswer((invocation) -> param.getValue());
//使用ArgumentCaptor捕获值
ArgumentCaptor param=ArgumentCaptor.forClass(String.class);
当(mock.myFunction(param.capture()))
.thenAnswer((调用)->param.getValue());

摘要:使用argumentcaptor捕获传递的参数。在后面的回答中,返回使用getValue捕获的值。

好的,一般来说,任何java模拟框架如何。。。这在任何其他框架中都是可能的,或者我应该创建一个哑存根来模仿我想要的行为吗?这也是我想要的。非常感谢。但我的问题不同。我想模拟存储对象并按名称返回它们的持久性服务(EJB)。因此,代码读起来像
when(…).then(Return.firstParameter())
使用Java 8 lambdas返回第一个参数非常简单,即使对于特定的类,也很容易,例如
when(foo(any()).then(i->i.getArgumentAt(0,Bar.class))
。你也可以使用方法引用和调用real方法。这解决了我的问题,方法返回
迭代器,使用Java 8和Mockito<1.9.5,然后Paweł的答案变成
when(foo(any())。然后回答(i->i.getArguments()[0])
注意:它是
when(…)。然后(returnsFirstArg())
,我在(…)时错误地使用了
。然后返回(returnsFirstArg())
它给出了
java.lang.ClassCastException:org.mockito.internal.stubing.answers.ReturnsArgumentAt不能转换为
注意:returnsFirstArg()返回的是答案,而不是参数的值。get'Foo(java.lang.String)不能应用于'(org.mockito.stubing.Answer)“在尝试调用.thenReturn(new Foo(returns firstarg()))时,在过去的几年中,我总是需要一次又一次地搜索这个答案,因为我记不起“其他答案”我很少需要它。然后我想知道我怎么能建立这样的场景,因为我找不到必要的依赖项。这不能直接添加到mockito吗?:/Steve的答案更一般。这只允许你返回原始参数。如果你想处理该参数并返回结果,那么Steve的答案就是规则。我对两者都投了赞成票,因为它们都很有用。仅供参考,我们必须导入
static org.mockito.AdditionalAnswers.returnsFirstArg
。这将使用returnsFirstArg。此外,我可以