3

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Aram Aslanyan
  • 745
  • 3
  • 11
  • 18
  • `String func(String arg){ return arg; }` – twentylemon Jun 23 '15 at 20:19
  • possible duplicate of [Mockito: How to make a method return an argument that was passed to it](http://stackoverflow.com/questions/2684630/mockito-how-to-make-a-method-return-an-argument-that-was-passed-to-it) – Jeff Bowman Jun 23 '15 at 21:14

5 Answers5

9

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());
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • Thanks for you response, it solves my problem! The second solution returns null, but the first solution fits perfectly. I have tried something similar: when(mock.testMethod(anyString())).thenReturn(returnsArgAt(0).toString()); But it doesn't work in my case, may be I did something wrong. – Aram Aslanyan Jun 23 '15 at 20:52
  • @Jean: Avoid `ClonesArguments`, as it's an internal package; AdditionalAnswers is the way to go there. Aram: `thenReturn` will always calculate and save a value at stubbing time, which is long before you need it. Using `thenAnswer(returnArgAt(0))` tells Mockito to _use an Answer to calculate the value_, specifically _the Answer given by `returnsArgAt(0)`_, so you shouldn't call `toString` there. – Jeff Bowman Jun 23 '15 at 21:13
  • @JeffBowman You are right, I did not notice it was internal. I removed it and switched to ``AdditionalAnswers``. – Jean Logeart Jun 23 '15 at 21:18
3

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

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.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
1

Put it like this:

public String testMethod(String arg){
    return arg;
}
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
1

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);
twentylemon
  • 1,248
  • 9
  • 11
  • 2
    No, this is _not_ what OPs wants. He wants to mock that method to return exactly that String he passes to that method. With "generic" he meant, that he don't want to use String literals for the `when(...).then(...)` statement. – Tom Jun 23 '15 at 20:46