Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
Return class from method in mocked method. Mockito

I have class TaskType. It has method

public Class<? extends TaskParameters> getTypeParameters() {
    return typeParameters;
}

next i want to mock this class and mock this method:

    final TaskType TEST_PARAMETERS = Mockito.mock(TaskType.class);
    when(TEST_PARAMETERS.getTypeParameters()).thenReturn(ScheduledParameters.class);
    Mockito.doReturn(4).when(TEST_PARAMETERS).ordinal();

but i got problem:

error: no suitable method found for thenReturn(java.lang.Class<com.ucp.shard.bulkops.service.executor.ScheduleExecutorTest.ScheduledParameters>)
        when(TEST_PARAMETERS.getTypeParameters()).thenReturn(ScheduledParameters.class);

Help, how can i mock this method?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your question is missing a lot of information, it would be nice if you could update it. For example, you could share the code for TaskType.

First of all, it seems that TaskType is an enum as you are trying to call ordinal(), right? If it's the case, you need to remember that enums are final and cannot be mocked by Mockito by default (please read here for more information). If it's not an enum you can ignore it. :)

Regarding the problem mocking getTypeParameters(), you cannot do it directly due to type erasure (more information here). However, you can solve it using Mockito Answer:

final TaskType TEST_PARAMETERS = Mockito.mock(TaskType.class);
final Answer<Class<ScheduledParameters>> answer = invocation -> ScheduledParameters.class;
when(TEST_PARAMETERS.getTypeParameters())
    .thenAnswer(answer);
Mockito.doReturn(4).when(TEST_PARAMETERS).ordinal();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!