代码之家  ›  专栏  ›  技术社区  ›  Thom

使用mockito[duplicate]模拟Spring存储库删除时无法应用

  •  1
  • Thom  · 技术社区  · 6 年前

    这个问题已经有了答案:

    • how to make mockto void methods with mockito 10个答案
      • 我搜索了所有我能想到的解决方案,但措辞很难。

        我有一个在Spring存储库中调用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());
        

        但是我得到了以下错误:

        错误文本:

        no instance of type variable(s)t exist so that void conformit to t
        

        不确定如何进行。 10个答案

    我在谷歌上搜索了所有我能想到的解决方案,但措辞很难。

    我有一个在Spring存储库中调用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 回复  |  直到 5 年前
        1
  •  1
  •   Thom    5 年前

    如前所述,问题实际上是返回是空的,而不是传递的参数类型。根据 How to make mock to void methods with Mockito ,我将代码更改如下:

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

    这解决了问题。