找到你要的答案

Q:Return argument of mocked method as a result

Q:作为结果的嘲笑方法的返回参数

The simplest example:I have the following method:

public String testMethod(String arg){
    .....
}

I want to mock this method to return passed argument as a result. For example:

testMethod("aString") returns "aString"
testMethod("anotherString") returns "anotherString"

I know I can hard code this behavior, but I want it generic.

最简单的例子:我有以下方法:

public String testMethod(String arg){
    .....
}

我想模拟这个方法作为结果返回参数。例如:

testMethod("aString") returns "aString"
testMethod("anotherString") returns "anotherString"

我知道我可以硬编码这个行为,但我希望它通用。

answer1: 回答1:

You can write your own Answer:

when(mock.testMethod(anyString())).thenAnswer(new Answer<String>() {
    @Override
    public String answer(InvocationOnMock invocation) {
        return invocation.getArgumentAt(0, String.class);
    }
});

Or:

when(mock.testMethod(anyString()))
    .thenAnswer(AdditionalAnswers.<String>returnsFirstArg());

你可以自己写答案:

when(mock.testMethod(anyString())).thenAnswer(new Answer<String>() {
    @Override
    public String answer(InvocationOnMock invocation) {
        return invocation.getArgumentAt(0, String.class);
    }
});

或:

when(mock.testMethod(anyString()))
    .thenAnswer(AdditionalAnswers.<String>returnsFirstArg());
answer2: 回答2:

You could implement your own Answer:

Answer<String> returnArgAnswer = new Answer<String>() {
    public String answer(InvocationOnMock invocationOnMock) throws Throwable {
        return (String) invocationOnMock.getArguments()[0];
    }
};

SomeClass x = mock(SomeClass.class);
Mockito.when(x.testMethod(anyString())).thenAnswer(returnArgAnswer);

Then, calling x.testMethod with any string will return that string.

你可以实现自己的答案:

Answer<String> returnArgAnswer = new Answer<String>() {
    public String answer(InvocationOnMock invocationOnMock) throws Throwable {
        return (String) invocationOnMock.getArguments()[0];
    }
};

SomeClass x = mock(SomeClass.class);
Mockito.when(x.testMethod(anyString())).thenAnswer(returnArgAnswer);

然后,任何字符串调用x.testmethod会返回字符串。

answer3: 回答3:

Put it like this:

public String testMethod(String arg){
    return arg;
}

把它像这样:

public String testMethod(String arg){
    return arg;
}
answer4: 回答4:

Generic-generic? You can use the following

public <T> T testMethod(T arg) {
    return arg;
}

Then you can use it with any type,

String s = testMethod("someString");
Integer i = testMethod(0);

通用?您可以使用以下

public <T> T testMethod(T arg) {
    return arg;
}

然后,您可以使用它与任何类型,

String s = testMethod("someString");
Integer i = testMethod(0);
answer5: 回答5:

This already exists built-in on Mockito 1.9.5 and later, as returnsFirstArg.

when(mock.testMethod(anyString())).thenAnswer(returnsFirstArg());

You may wish to avoid using ClonesArguments directly, as it's in the internal package org.mockito.internal.stubbing.answers. Mockito's static factory classes are the preferred way to get to Mockito's predefined answers and matchers.

这已经存在内置在mockito 1.8.5,后来,作为returnsfirstarg。

when(mock.testMethod(anyString())).thenAnswer(returnsFirstArg());

你可能希望避免直接使用clonesarguments,作为它的内部包装org.mockito.internal.stubbing.answers。Mockito的静态工厂类去mockito预定义的答案和匹配的首选方式。

java  junit  mockito