Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

259
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda