Mockito方法调用链

  @PostMapping("/node{id}/delete")
  public ResponseEntity<CallbackResponse> delete(@RequestParam("eventId") String eventId, @RequestParam("id") String nodeId)) throws ResourceNotFoundException {

      serviceA.remove(eventId);
      client.getServiceB().deleteById(nodeId);
      return new ResponseEntity<>(new CallbackResponse(true), HttpStatus.OK);
  }

我是单元测试的新手。 请帮助我编写上述REST控制器方法的测试用例

我尝试进行以下测试,但无法正常工作

    @Test
    public void testDeleteAgent() throws Exception{
        ServiceA serviceA = Mockito.mock(ServiceA.class);
        NodeClient client = Mockito.mock(NodeClient.class);

        Mockito.doNothing.when(serviceA).remove(Mockito.any());
        Mockito.doNothing.when(client.getServiceB()).deleteById(Mockito.any()
        ResponseEntity<CallbackResponse> result = agentController.delete("123", "456");
        Assert.assertEquals(HttpStatus.OK.value(), result.getStatusCode());
    }

当我通过测试时,我在这里检测到未完成的存根错误

评论
  • ?Mate
    ?Mate 回复

    Supposing there is somewhere a ServiceB class, let's try

    
    ServiceB serviceB = Mockito.mock(ServiceB.class);
    Mockito.doReturn(serviceB).when(client).getServiceB();
    Mockito.doNothing().when(serviceB).deleteById(Mockito.any());
    
    // other here