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 6670401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:15:52+00:00 2026-05-26T03:15:52+00:00

Is there a way to match any class argument of the below sample routine?

  • 0

Is there a way to match any class argument of the below sample routine?

class A {
     public B method(Class<? extends A> a) {}
}

How can I always return a new B() regardless of which class is passed into method? The following attempt only works for the specific case where A is matched.

A a = new A();
B b = new B();
when(a.method(eq(A.class))).thenReturn(b);

EDIT: One solution is

(Class<?>) any(Class.class)
  • 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-05-26T03:15:53+00:00Added an answer on May 26, 2026 at 3:15 am

    Two more ways to do it (see my comment on the previous answer by @Tomasz Nurkiewicz):

    The first relies on the fact that the compiler simply won’t let you pass in something of the wrong type:

    when(a.method(any(Class.class))).thenReturn(b);
    

    You lose the exact typing (the Class<? extends A>) but it probably works as you need it to.

    The second is a lot more involved but is arguably a better solution if you really want to be sure that the argument to method() is an A or a subclass of A:

    when(a.method(Matchers.argThat(new ClassOrSubclassMatcher<A>(A.class)))).thenReturn(b);
    

    Where ClassOrSubclassMatcher is an org.hamcrest.BaseMatcher defined as:

    public class ClassOrSubclassMatcher<T> extends BaseMatcher<Class<T>> {
    
        private final Class<T> targetClass;
    
        public ClassOrSubclassMatcher(Class<T> targetClass) {
            this.targetClass = targetClass;
        }
    
        @SuppressWarnings("unchecked")
        public boolean matches(Object obj) {
            if (obj != null) {
                if (obj instanceof Class) {
                    return targetClass.isAssignableFrom((Class<T>) obj);
                }
            }
            return false;
        }
    
        public void describeTo(Description desc) {
            desc.appendText("Matches a class or subclass");
        }       
    }
    

    Phew! I’d go with the first option until you really need to get finer control over what method() actually returns 🙂

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

Sidebar

Related Questions

Is there any way to handle the exception if regex.Matches didn't match anything and
Is there a way to match the following string with any of the hamcrest
Is there any convenient way to type in braces in Math in Latex? Especially
Is there a way to match a pattern ( e\d\d ) several times, capturing
Is there a way to match Hex/RGB colors to the closest PMS (Pantone Matching
Is there a way to tell preg_match_all to use the third match it finds
Is there a way to determine if a MATCH query sent to an fts3
I am wondering if there is a way to check a subpattern match for
There is no setBackground() method in the RemoteViews class, so I've used the following
Is there any way to compare two functions in Haskell? My thought is that

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.