mocking a method that return generics with wildcard using mockitoHow to handle different return type of a method in then Return?Mockito: Stubbing Methods That Return Type With Bounded Wild-Cardsmock method with generic and extends in return typeCreate Generic method constraining T to an EnumHow do I use reflection to call a generic method?How do I generate random integers within a specific range in Java?How to make mock to void methods with MockitoInjecting Mockito mocks into a Spring beanMaking a mocked method return an argument that was passed to itHow to verify that a specific method was not called using Mockito?Mocking static methods with MockitoMockito error with method that returns Optional<T>Using mockito to mock a service with generic methods within

Help, I cannot decide when to start the story

Suspension compromise for urban use

How can I shoot a bow using Strength instead of Dexterity?

Do I have to cite common CS algorithms?

What are the odds of rolling specific ability score totals in D&D?

Why won't the Republicans use a superdelegate system like the DNC in their nomination process?

Can the average speed of a moving body be 0?

Would the USA be eligible to join the European Union?

Will using a resistor in series with a LED to control its voltage increase the total energy expenditure?

To show that a recursively defined sequence is Cauchy - How?

If a person claims to know anything could it be disproven by saying 'prove that we are not in a simulation'?

How do I call a 6-digit Australian phone number with a US-based mobile phone?

What can Amex do if I cancel their card after using the sign up bonus miles?

Good textbook for queueing theory and performance modeling

Unconventional examples of mathematical modelling

How would armour (and combat) change if the fighter didn't need to actually wear it?

Are there examples in Tanach of 3 or more parties having an ongoing conversation?

Is this bar slide trick shown on Cheers real or a visual effect?

What would it take to get a message to another star?

What is the most difficult concept to grasp in Calculus 1?

"Mouth-breathing" as slang for stupidity

Doesn't the speed of light limit imply the same electron can be annihilated twice?

How much can I judge a company based on a phone screening?

Word for an event that will likely never happen again



mocking a method that return generics with wildcard using mockito


How to handle different return type of a method in then Return?Mockito: Stubbing Methods That Return Type With Bounded Wild-Cardsmock method with generic and extends in return typeCreate Generic method constraining T to an EnumHow do I use reflection to call a generic method?How do I generate random integers within a specific range in Java?How to make mock to void methods with MockitoInjecting Mockito mocks into a Spring beanMaking a mocked method return an argument that was passed to itHow to verify that a specific method was not called using Mockito?Mocking static methods with MockitoMockito error with method that returns Optional<T>Using mockito to mock a service with generic methods within






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








46















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?










share|improve this question
























  • You shouldn't have the wildcard in the returntype of getMyInterfaces. That's not a good practice, because clients of that api have to deal with a wildcard where they do not know anything about.

    – SpaceTrucker
    Apr 11 '13 at 7:35






  • 1





    I don't control this API. It's given by a 3rd party.

    – user1504992
    Apr 11 '13 at 7:59






  • 5





    @SpaceTrucker I think having a return type like List<? extends MyInterface> is a perfectly sensible thing to do. It means that you (as the method caller) can get MyInterface objects out of the list, but you can't put anything in it.

    – Paŭlo Ebermann
    Nov 17 '14 at 10:13

















46















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?










share|improve this question
























  • You shouldn't have the wildcard in the returntype of getMyInterfaces. That's not a good practice, because clients of that api have to deal with a wildcard where they do not know anything about.

    – SpaceTrucker
    Apr 11 '13 at 7:35






  • 1





    I don't control this API. It's given by a 3rd party.

    – user1504992
    Apr 11 '13 at 7:59






  • 5





    @SpaceTrucker I think having a return type like List<? extends MyInterface> is a perfectly sensible thing to do. It means that you (as the method caller) can get MyInterface objects out of the list, but you can't put anything in it.

    – Paŭlo Ebermann
    Nov 17 '14 at 10:13













46












46








46


10






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?










share|improve this question














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?







java generics mockito






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 11 '13 at 7:21









user1504992user1504992

2051 gold badge3 silver badges8 bronze badges




2051 gold badge3 silver badges8 bronze badges















  • You shouldn't have the wildcard in the returntype of getMyInterfaces. That's not a good practice, because clients of that api have to deal with a wildcard where they do not know anything about.

    – SpaceTrucker
    Apr 11 '13 at 7:35






  • 1





    I don't control this API. It's given by a 3rd party.

    – user1504992
    Apr 11 '13 at 7:59






  • 5





    @SpaceTrucker I think having a return type like List<? extends MyInterface> is a perfectly sensible thing to do. It means that you (as the method caller) can get MyInterface objects out of the list, but you can't put anything in it.

    – Paŭlo Ebermann
    Nov 17 '14 at 10:13

















  • You shouldn't have the wildcard in the returntype of getMyInterfaces. That's not a good practice, because clients of that api have to deal with a wildcard where they do not know anything about.

    – SpaceTrucker
    Apr 11 '13 at 7:35






  • 1





    I don't control this API. It's given by a 3rd party.

    – user1504992
    Apr 11 '13 at 7:59






  • 5





    @SpaceTrucker I think having a return type like List<? extends MyInterface> is a perfectly sensible thing to do. It means that you (as the method caller) can get MyInterface objects out of the list, but you can't put anything in it.

    – Paŭlo Ebermann
    Nov 17 '14 at 10:13
















You shouldn't have the wildcard in the returntype of getMyInterfaces. That's not a good practice, because clients of that api have to deal with a wildcard where they do not know anything about.

– SpaceTrucker
Apr 11 '13 at 7:35





You shouldn't have the wildcard in the returntype of getMyInterfaces. That's not a good practice, because clients of that api have to deal with a wildcard where they do not know anything about.

– SpaceTrucker
Apr 11 '13 at 7:35




1




1





I don't control this API. It's given by a 3rd party.

– user1504992
Apr 11 '13 at 7:59





I don't control this API. It's given by a 3rd party.

– user1504992
Apr 11 '13 at 7:59




5




5





@SpaceTrucker I think having a return type like List<? extends MyInterface> is a perfectly sensible thing to do. It means that you (as the method caller) can get MyInterface objects out of the list, but you can't put anything in it.

– Paŭlo Ebermann
Nov 17 '14 at 10:13





@SpaceTrucker I think having a return type like List<? extends MyInterface> is a perfectly sensible thing to do. It means that you (as the method caller) can get MyInterface objects out of the list, but you can't put anything in it.

– Paŭlo Ebermann
Nov 17 '14 at 10:13












2 Answers
2






active

oldest

votes


















58














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/

  • ...





share|improve this answer



























  • Thanks also for the insightful links!

    – thSoft
    Oct 1 '13 at 15:02











  • The doReturn syntax is not type-safe, though (you could return anything, not just a list of something). (Though you then get an exception still during this setup – but not if you put in a different kind of list.)

    – Paŭlo Ebermann
    Nov 17 '14 at 10:36












  • @PaŭloEbermann Indeed, this is due to type erasure. In this case the check is done at runtime. The other form can be type checked at compile time.

    – Brice
    Nov 17 '14 at 12:55


















45














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);





share|improve this answer




















  • 10





    This is black magic, but it works perfectly. Thank you

    – Sebastian Wramba
    Jan 15 '15 at 7:35











  • This is the only one that worked for me.

    – gladed
    May 31 '17 at 23:18













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15942880%2fmocking-a-method-that-return-generics-with-wildcard-using-mockito%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









58














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/

  • ...





share|improve this answer



























  • Thanks also for the insightful links!

    – thSoft
    Oct 1 '13 at 15:02











  • The doReturn syntax is not type-safe, though (you could return anything, not just a list of something). (Though you then get an exception still during this setup – but not if you put in a different kind of list.)

    – Paŭlo Ebermann
    Nov 17 '14 at 10:36












  • @PaŭloEbermann Indeed, this is due to type erasure. In this case the check is done at runtime. The other form can be type checked at compile time.

    – Brice
    Nov 17 '14 at 12:55















58














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/

  • ...





share|improve this answer



























  • Thanks also for the insightful links!

    – thSoft
    Oct 1 '13 at 15:02











  • The doReturn syntax is not type-safe, though (you could return anything, not just a list of something). (Though you then get an exception still during this setup – but not if you put in a different kind of list.)

    – Paŭlo Ebermann
    Nov 17 '14 at 10:36












  • @PaŭloEbermann Indeed, this is due to type erasure. In this case the check is done at runtime. The other form can be type checked at compile time.

    – Brice
    Nov 17 '14 at 12:55













58












58








58







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/

  • ...





share|improve this answer















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/

  • ...






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 2 '18 at 13:27

























answered Apr 11 '13 at 9:16









BriceBrice

26.3k6 gold badges68 silver badges84 bronze badges




26.3k6 gold badges68 silver badges84 bronze badges















  • Thanks also for the insightful links!

    – thSoft
    Oct 1 '13 at 15:02











  • The doReturn syntax is not type-safe, though (you could return anything, not just a list of something). (Though you then get an exception still during this setup – but not if you put in a different kind of list.)

    – Paŭlo Ebermann
    Nov 17 '14 at 10:36












  • @PaŭloEbermann Indeed, this is due to type erasure. In this case the check is done at runtime. The other form can be type checked at compile time.

    – Brice
    Nov 17 '14 at 12:55

















  • Thanks also for the insightful links!

    – thSoft
    Oct 1 '13 at 15:02











  • The doReturn syntax is not type-safe, though (you could return anything, not just a list of something). (Though you then get an exception still during this setup – but not if you put in a different kind of list.)

    – Paŭlo Ebermann
    Nov 17 '14 at 10:36












  • @PaŭloEbermann Indeed, this is due to type erasure. In this case the check is done at runtime. The other form can be type checked at compile time.

    – Brice
    Nov 17 '14 at 12:55
















Thanks also for the insightful links!

– thSoft
Oct 1 '13 at 15:02





Thanks also for the insightful links!

– thSoft
Oct 1 '13 at 15:02













The doReturn syntax is not type-safe, though (you could return anything, not just a list of something). (Though you then get an exception still during this setup – but not if you put in a different kind of list.)

– Paŭlo Ebermann
Nov 17 '14 at 10:36






The doReturn syntax is not type-safe, though (you could return anything, not just a list of something). (Though you then get an exception still during this setup – but not if you put in a different kind of list.)

– Paŭlo Ebermann
Nov 17 '14 at 10:36














@PaŭloEbermann Indeed, this is due to type erasure. In this case the check is done at runtime. The other form can be type checked at compile time.

– Brice
Nov 17 '14 at 12:55





@PaŭloEbermann Indeed, this is due to type erasure. In this case the check is done at runtime. The other form can be type checked at compile time.

– Brice
Nov 17 '14 at 12:55













45














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);





share|improve this answer




















  • 10





    This is black magic, but it works perfectly. Thank you

    – Sebastian Wramba
    Jan 15 '15 at 7:35











  • This is the only one that worked for me.

    – gladed
    May 31 '17 at 23:18















45














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);





share|improve this answer




















  • 10





    This is black magic, but it works perfectly. Thank you

    – Sebastian Wramba
    Jan 15 '15 at 7:35











  • This is the only one that worked for me.

    – gladed
    May 31 '17 at 23:18













45












45








45







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);





share|improve this answer













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);






share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 1 '13 at 14:44









thSoftthSoft

16.3k5 gold badges77 silver badges94 bronze badges




16.3k5 gold badges77 silver badges94 bronze badges










  • 10





    This is black magic, but it works perfectly. Thank you

    – Sebastian Wramba
    Jan 15 '15 at 7:35











  • This is the only one that worked for me.

    – gladed
    May 31 '17 at 23:18












  • 10





    This is black magic, but it works perfectly. Thank you

    – Sebastian Wramba
    Jan 15 '15 at 7:35











  • This is the only one that worked for me.

    – gladed
    May 31 '17 at 23:18







10




10





This is black magic, but it works perfectly. Thank you

– Sebastian Wramba
Jan 15 '15 at 7:35





This is black magic, but it works perfectly. Thank you

– Sebastian Wramba
Jan 15 '15 at 7:35













This is the only one that worked for me.

– gladed
May 31 '17 at 23:18





This is the only one that worked for me.

– gladed
May 31 '17 at 23:18

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15942880%2fmocking-a-method-that-return-generics-with-wildcard-using-mockito%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해