7

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.

Thom
  • 14,013
  • 25
  • 105
  • 185
  • Please paste the text of error, image is not visible behind firewall. –  Jun 14 '18 at 17:02
  • `Exception` is a checked exception. Unless your `delete() ` method declares `throws Exception`, you can't throw it from there. Use `RuntimeException` –  Jun 14 '18 at 17:03
  • @Arkadiy switched to RuntimeException. No change. – Thom Jun 14 '18 at 17:15
  • OK, then I need to see the text of the error. Image is filtered out :( –  Jun 14 '18 at 17:16
  • @Arkadiy Added. Thanks for your interest. – Thom Jun 14 '18 at 17:17
  • Ah! it's `void delete(...)`, isn't it? Try this https://stackoverflow.com/questions/2276271/how-to-make-mock-to-void-methods-with-mockito –  Jun 14 '18 at 17:18
  • @Arkadiy That was it, thanks! Can you post this as an answer so I can give you credit? – Thom Jun 14 '18 at 17:25

1 Answers1

18

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.

Thom
  • 14,013
  • 25
  • 105
  • 185