Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mocking a method that return generics with wildcard using mockito

I'm using mockito 1.9.5. I have the following code:

public class ClassA  {  public List<? extends MyInterface> getMyInterfaces() {     return null; }  public static void testMock() {     List<MyInterface> interfaces = new ArrayList<>();     ClassA classAMock = mock(ClassA.class);     when(classAMock.getMyInterfaces()).thenReturn(interfaces);       } 

I get a compilation error for the thenReturn(interfaces) saying:

"The method thenReturn(List<capture#1-of ? extends MyInterface>) in the type   OngoingStubbing<List<capture#1-of ? extends MyInterface>> is not applicable for the arguments   (List<MyInterface>)" 

However, when I use the thenAnswer method of mockito, I don't get the error. Can anyone tell me what's going on? Why do I get the error when I use the thenReturn method? Is there any other way to solve this problem when ClassA is provided by a 3rd party and cannot be modified?

like image 856
user1504992 Avatar asked Apr 11 '13 07:04

user1504992


People also ask

Does Mockito have a return method?

In Mockito, you can specify what to return when a method is called. That makes unit testing easier because you don't have to change existing classes. Mockito supports two ways to do it: when-thenReturn and doReturn-when . In most cases, when-thenReturn is used and has better readability.

Can we mock public methods using Mockito?

With Mockito, you create a mock, tell Mockito what to do when specific methods are called on it, and then use the mock instance in your test instead of the real thing. After the test, you can query the mock to see what specific methods were called or check the side effects in the form of changed state.

What can be mocked with Mockito?

Mockito mock method We can use Mockito class mock() method to create a mock object of a given class or interface. This is the simplest way to mock an object. We are using JUnit 5 to write test cases in conjunction with Mockito to mock objects.

What does Mockito mock return?

Mockito when() method It should be used when we want to mock to return specific values when particular methods are called. In simple terms, "When the XYZ() method is called, then return ABC." It is mostly used when there is some condition to execute.


2 Answers

EDIT : Starting from Mockito 1.10.x, generics types that are embedded in the class are now used by Mockito for deep stubs. ie.

public interface A<T extends Observer & Comparable<? super T>>  {   List<? extends B> bList();   T observer(); }  B b = deep_stubbed.bList().iterator().next(); // returns a mock of B ; mockito remebers that A returns a List of B Observer o = deep_stubbed.observer(); // mockito can find that T super type is Observer Comparable<? super T> c = deep_stubbed.observer(); // or that T implements Comparable 

Mockito tries its best to get type information that the compiler embeds, but when erasure applies, mockito cannot do anything but return a mock of Object.


Original : Well that's more of an issue with generics than with Mockito. For generics, you should read what Angelika Langer wrote on them. And for the current topic, i.e. wildcards, read this section.

But for short, what you could use is the other syntax of Mockito to help with your current situation :

doReturn(interfaces).when(classAMock).getMyInterfaces(); 

Or with the BDD aliases :

willReturn(interfaces).given(classAMock).getMyInterfaces(); 

Nevertheless, you could write wrappers that are more generic friendly. That will help future developers working with same 3rd party API.


As a side note: you shouldn't mocks type you don't own, it can lead to many errors and issues. Instead you should have some wrapper. DAO and repositories for example represent such idea, one will mock the DAO or repository interface, but not the JDBC / JPA / hibernate stuff. There are many blog posts about that:

  • http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html
  • http://blog.8thlight.com/eric-smith/2011/10/27/thats-not-yours.html
  • https://web.archive.org/web/20140923101818/http://freshbrewedcode.com/derekgreer/2012/04/01/tdd-best-practices-dont-mock-others/
  • ...
like image 140
Brice Avatar answered Oct 04 '22 02:10

Brice


Another solution (albeit less readable) is to qualify the static method call of when to bind the wildcard:

Mockito.<List<? extends MyInterface>>when(classAMock.getMyInterfaces()).thenReturn(interfaces); 
like image 23
thSoft Avatar answered Oct 04 '22 00:10

thSoft