Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:43:20+00:00 2026-06-04T07:43:20+00:00

Consider this code: public class DummyClass { public List<? extends Number> dummyMethod() { return

  • 0

Consider this code:

public class DummyClass {
    public List<? extends Number> dummyMethod() {
        return new ArrayList<Integer>();
    }
}
public class DummyClassTest {
    public void testMockitoWithGenerics() {
        DummyClass dummyClass = Mockito.mock(DummyClass.class);
        List<? extends Number> someList = new ArrayList<Integer>();
        Mockito.when(dummyClass.dummyMethod()).thenReturn(someList); //Compiler complains about this
    }
}

The compiler complains about the line that’s trying to stub the behavior for dummyMethod(). Any pointers on how one goes about stubbing methods that return a type with bounded wild-cards?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:43:21+00:00Added an answer on June 4, 2026 at 7:43 am

    You can also use the non-type safe method doReturn for this purpose,

    @Test
    public void testMockitoWithGenerics()
    {
        DummyClass dummyClass = Mockito.mock(DummyClass.class);
        List<? extends Number> someList = new ArrayList<Integer>();
    
        Mockito.doReturn(someList).when(dummyClass).dummyMethod();
    
        Assert.assertEquals(someList, dummyClass.dummyMethod());
    }
    

    as discussed on Mockito’s google group.

    While this is simpler than thenAnswer, again note that it is not type safe. If you’re concerned about type safety, millhouse’s answer is correct.

    Additional Details

    To be clear, here’s the observed compiler error,

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

    I believe the compiler has assigned the first wildcard type during the when call and then cannot confirm that the second wildcard type in the thenReturn call is the same.

    It looks like thenAnswer doesn’t run into this issue because it accepts a wildcard type while thenReturn takes a non-wildcard type, which must be captured. From Mockito’s OngoingStubbing,

    OngoingStubbing<T> thenAnswer(Answer<?> answer);
    OngoingStubbing<T> thenReturn(T value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return new
Consider this code snippet: class MyClass{ private List myList; //... public List getList(){ return
Consider this code: package Prova; import java.util.ArrayList; public class Prova { private ArrayList<String> people;
consider this code : Class Controller_xyz extends Controller { protected $res=' '; public function
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Consider this code sample: public class Human { public string Value { get; set;}
consider this code: Partial Public Class MyAspNETClass Inherits System.Web.UI.Page Protected Shared MyVariable As String
Consider this code snippet: public static class ApplicationContext { private static Func<TService> Uninitialized<TService>() {
Consider this piece of code: public abstract class Validator { protected Validator() { }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.