考虑如下方法签名:

public String myFunction(String abc);

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


当前回答

这是一个很老的问题,但我认为仍然相关。此外,接受的答案仅适用于字符串。同时还有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应用程序中使用mock。我在这里发布了一个示例,以供参考,并与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"))
}

自Mockito 1.9.5+和Java 8以来+

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

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

其中i是InvocationOnMock的实例。

对于旧版本

您可以在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 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;
    }  
}

使用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捕获的值。