代码之家  ›  专栏  ›  技术社区  ›  Indresh Mahor

当从控制器调用测试控制器和功能接口时,无法模拟功能接口

  •  0
  • Indresh Mahor  · 技术社区  · 8 月前

    SomeClass instance = someService.getSomething.apply("by-something");

    上述语句正在控制器中用于获取数据。我想测试控制器。所以,我在下面的测试中嘲笑线以上。

    @Mock
    private SomeService someService;
    
    @Mock
    private Function<String, SomeTrigger> someTriggerFunction;
    
    
    when(someService.getSomething.apply("by-something")).thenReturn(someTrigger);
    
    

    现在它给了我以下错误

    org.mockito.exceptions.mising。MissingMethodInvocation异常: when()需要一个参数,该参数必须是“对模拟的方法调用”。 例如: when(mock.getArticles()).then return(articles);

    此外,出现此错误的原因可能是:

    1. 您可以存根以下任一方法:final/private/national/equals()/hashCode()方法。 这些方法 不能 被打断/验证。 不支持在非公共父类上声明的模拟方法。
    2. 在when()中,你不是在mock上调用方法,而是在其他对象上调用方法。
    1 回复  |  直到 8 月前
        1
  •  1
  •   Olivier Boissé    8 月前

    你应该设置 getSomething 场在 someService 这样地:

    @Mock
    private SomeService someService;
    
    @Mock 
    private Function<String, SomeTrigger> someTriggerFunction;
    
    ...
    
    someService.getSomething = someTriggerFunction;
    
    when(someTriggerFunction.apply("by-something")).thenReturn(someTrigger);