Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot apply when mocking spring repository delete with Mockito [duplicate]

I googled all I could think of for solutions, but phrasing is difficult.

I have a unit test that calls delete on a Spring Repository. The repo is defined as:

public interface FileConfigurationRepository extends CasenetRepository<FileConfiguration, String> {}

The method I'm testing has the following call:

    fileConfigurationRepository.delete(GlobalConfiguration.CUSTOM_LOGO_ID);

Where GlobalConfiguration.CUSTOM_LOGO_ID is defined as:

public static final String CUSTOM_LOGO_ID = "customLogoId";

So I wrote my mock as follows:

  Mockito.when(fileConfigurationRepository.delete(GlobalConfiguration.CUSTOM_LOGO_ID)).thenThrow(new Exception());

But then I get the following error:

enter image description here

The text of the error:

No instance(s) of type variable(s) T exist so that void conforms to T

Unsure how to proceed.

like image 617
Thom Avatar asked Jun 14 '18 16:06

Thom


1 Answers

As indicated, the issue was really about the return being void and not about the parameter type being passed. According to How to make mock to void methods with Mockito, I changed the code as follows:

    Mockito.doThrow(new RuntimeException()).when(fileConfigurationRepository).delete(GlobalConfiguration.CUSTOM_LOGO_ID);

And that fixed the problem.

like image 77
Thom Avatar answered Oct 17 '22 01:10

Thom