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

  • Home
  • SEARCH
  • 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 8061357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:18:53+00:00 2026-06-05T10:18:53+00:00

I’m writing a test case that uses a java.beans.PropertyDescriptor using Mockito, and I want

  • 0

I’m writing a test case that uses a java.beans.PropertyDescriptor using Mockito, and I want to mock the behavior of getPropertyType() to return an arbitrary Class<?> object (in my case, String.class). Normally, I would do that by just invoking:

// we already did an "import static org.mockito.Mockito.*"
when(mockDescriptor.getPropertyType()).thenReturn(String.class);

However, oddly, this does not compile:

cannot find symbol method thenReturn(java.lang.Class<java.lang.String>)

But when I specify the type parameter instead of depending on inference:

Mockito.<Class<?>>when(mockDescriptor.getPropertyType()).thenReturn(String.class);

everything is hunky dory. Why can’t the compiler correctly infer the return type of when() in this case? I have never had to specify the parameter before like that.

  • 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-05T10:18:54+00:00Added an answer on June 5, 2026 at 10:18 am

    PropertyDescriptor#getPropertyType() returns an object of Class<?>, where the ? means “this is a type, but I don’t know what it is”. Let’s call this type “X”. So when(mockDescriptor.getPropertyType()) creates an OngoingStubbing<Class<X>>, whose method thenReturn(Class<X>) can only accept objects of Class<X>. But the compiler doesn’t know what type this “X” is, so it will complain about you passing in a Class of any type. I think this is the same reason the compiler complains about calling add(...) on a Collection<?>.

    When you explicitly specify Class<?> for the type on the when method, you’re not saying that mockDescriptor.getPropertyType() returns a Class<?>, you’re saying that when returns an OngoingStubbing<Class<?>>. Then, the compiler checks to make sure whatever you’re passing into when is of a type that matches Class<?>; since getPropertyType() returns the “Class<X>” I mentioned earlier, it of course matches the Class<?> you specified.

    So basically

    // the inferred type is Class<"some type">
    Mockito.when(mockDescriptor.getPropertyType())
    
    // the specified type is Class<"any type">
    Mockito.<Class<?>>when(mockDescriptor.getPropertyType())
    

    In my IDE, the error message for your original code is

    The method thenReturn(Class<capture#1-of ?>) in the type OngoingStubbing<Class<capture#1-of ?>> is not applicable for the arguments (Class<String>)
    

    That capture#1-of ? is the “X” I described above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

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.