使用Mockito模拟spring存储库删除时无法应用

时间:2018-06-14 16:50:44

标签: java unit-testing mockito

我用谷歌搜索了所有我想到的解决方案,但是语法很难。

我有一个单元测试,它在Spring Repository上调用delete。回购定义为:

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

我正在测试的方法有以下调用:

    fileConfigurationRepository.delete(GlobalConfiguration.CUSTOM_LOGO_ID);

其中GlobalConfiguration.CUSTOM_LOGO_ID定义为:

public static final String CUSTOM_LOGO_ID = "customLogoId";

所以我写了我的模拟如下:

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

但后来我收到以下错误:

enter image description here

错误的文字:

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

不确定如何继续。

1 个答案:

答案 0 :(得分:1)

如上所述,问题实际上是返回无效而不是传递的参数类型。根据{{​​3}},我将代码修改如下:

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

这解决了这个问题。