reason: no instance(s) of type variable(s) T exist so that void conforms to using mockito

27,877

Solution 1

For void methods, I think you need to use the doThrow syntax.

So in your case it would be:

doThrow(BookingException.builder().build())
      .when(booking)
      .validate(any());

I hope this helps.

Solution 2

I Figured out the right syntax.

Service mockedService = new DefaultServie();
doNothing().when(mockedService).sendReportingLogs(null);

Hope this answers the questions

Solution 3

/**
 * Use <code>doThrow()</code> when you want to stub the void method with an exception.
 * <p>
 * Stubbing voids requires different approach from {@link Mockito#when(Object)} because the compiler
 * does not like void methods inside brackets...
 * <p>
 * Example:
 *
 * <pre class="code"><code class="java">
 *   doThrow(new RuntimeException()).when(mock).someVoidMethod();
 * </code></pre>
 *
 * @param toBeThrown to be thrown when the stubbed method is called
 * @return stubber - to select a method for stubbing
 */
@CheckReturnValue
public static Stubber doThrow(Throwable... toBeThrown) {
    return MOCKITO_CORE.stubber().doThrow(toBeThrown);
}

Solution 4

Just complementing @Shane 's comment, which was a life saver but not very clear.

Example on how to throw PersistenceException from a repository method:

doThrow(new PersistenceException())
            .when(outboxRepository)
            .delete(isA(WorkersEphemeralOutboxEntry.class));

I find this especially helpful when you have overloaded methods and the compiler cannot figure out which one to call with any() and any(Class.class) gives you compiling error.

Share:
27,877
Sandro Rey
Author by

Sandro Rey

Bendiciones y unos Nullpointers. y si no sabes la respuesta a mis preguntas que te den por el xxxx dos veces.

Updated on July 09, 2022

Comments

  • Sandro Rey
    Sandro Rey almost 2 years

    I want to throw an Exception when running a void method

    when(booking.validate(any())).thenThrow(BookingException.builder().build());
    

    but I have a compilation error:

    Required type: T
    Provided: void
    reason: no instance(s) of type variable(s) T exist so that void conforms to T
    
  • Adam Hughes
    Adam Hughes about 3 years
    God imagine the man-years saved if this mockito error simply provided this as a hint! I hate java more and more...
  • Luca Masera
    Luca Masera over 2 years
    While doThrow is the correction function to handle the problem, please provide also some code to show how to use it, so you provide a concrete answer to the question.
  • mojo
    mojo over 2 years
    This tripped me up, and this helped. Thanks.
  • BabyishTank
    BabyishTank about 2 years
    bruh, this should just be in the document instead of what they have now
  • Valerij Dobler
    Valerij Dobler about 2 years
    @AdamHughes sadly it's a mismatch in signatures and can not be hinted more than it already does. Excerpt of the java-doc of the when method: For stubbing void methods with throwables see: doThrow(Throwable...)
  • Adam Hughes
    Adam Hughes about 2 years
    reason: no instance(s) of type variable(s) T exist so that void conforms to T could just end with ... did you mean doThrow() ?
  • M-sAnNan
    M-sAnNan almost 2 years
    @BabyishTank yeah right,