Mockito: How to mock a methods to get a parameter and return a specific value every time it’s called?

[cc lang=java]
PortBuildNumberGenerator mockBuildNumberGenerator = mock(PortBuildNumberGenerator.class);
when(mockBuildNumberGenerator.generateNextPortBuildNumberInSequence(anyString())).thenAnswer(new Answer() {

@Override
public String answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
return Integer.toString(Integer.parseInt((String)args[0]) + 1);
}
});
[/cc]