1

I have this method

public <T, R> R deepCopy(T source, R destination) {
   beanMapper.map(source, destination);
   return destination;
}

and want to mock with different method params like

mock.deepCopy(classA(), classB()).thenReturn(classB());
mock.deepCopy(classB(), classC()).thenReturn(classC());

but getting class cast exception.

lucid
  • 2,722
  • 1
  • 12
  • 24

1 Answers1

1

How about this

doAnswer(invocation -> {
       Object arg1 = invocation.getArguments()[0];
       Object arg2 = invocation.getArguments()[1];

       if(arg1 instanceof Integer && arg2 instanceof String)
           return "something";
        if(arg1 instanceof String && arg2 instanceof Boolean)
            return false;

        return false;
}).when(yourmock).deepCopy(any(), any());

Now if you pass call the method with arguments (1, "abcd"), the mock willreturn "something". And if you pass ("abcd", true) then it returns false

pvpkiran
  • 25,582
  • 8
  • 87
  • 134