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.

7
  • Please paste the text of error, image is not visible behind firewall. Commented Jun 14, 2018 at 17:02
  • Exception is a checked exception. Unless your delete() method declares throws Exception, you can't throw it from there. Use RuntimeException Commented Jun 14, 2018 at 17:03
  • @Arkadiy switched to RuntimeException. No change. Commented Jun 14, 2018 at 17:15
  • OK, then I need to see the text of the error. Image is filtered out :( Commented Jun 14, 2018 at 17:16
  • @Arkadiy Added. Thanks for your interest. Commented Jun 14, 2018 at 17:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.