如何在Mocked类中调用抽象方法的真正实现?

时间:2019-11-29 19:26:47

标签: java stream mocking mockito

我有一个RequestImpl类,它向呼叫者发送响应。 sendResponse()方法调用buildResponse()中的PrepareResponse方法,该方法调用抽象finalBuildResponse()(不是公共方法)。但是我不确定如何使用Mockito调用抽象finalBuildResponse()的实现。 finalBuildResponse()的实现是在构建项目时自动生成的,并且位于PrepareResponseImpl类中。

class RequestImpl {
    @Inject PrepareResponse prepareResponse;

    public void sendResponse(Map < String, Long > result) {
        ProductResponse pr = buildProductResponseObject();
        prepareResponse.buildResponse(pr, result)
    }
}

class PrepareResponse {

    abstract finalBuildResponse(ProductResponse obj, Response obj); // this implementation is  generated automatically , also please note this is not a public method

    public void buildResponse(pr, Map < String, Long > result) {
        for (String key: result.keySet()) {
            Response res = build(key, result.get(key));
            finalBuildResponse(pr, res); // how to call real method of abstract method buildResponse
        }

    }

    public Response build(String key, Long val) {
        Response res = new Response();
        // some logic to set values in response
        return res;
    }
}

class PrepareResponseImpl extends PrepareResponse {
    void finalBuildResponse(ProductResponse obj, Response obj) {
        // some logic
        return obj;
    }
}

和下面的测试类

class RequestImplTest{

      @Mock PrepareResponse prepareResponse;
      @InjectMocks RequestIpml impl;

      @Before
      public void setUp(){
              MockitoAnnotations.initMocks(this);
              doCallRealMethod().when(prepareResponse).buildResponse(any(), any());
              doCallRealMethod().when(prepareResponse).build(anyString(), anyLong());
         // **how to call real implementation of abstract method finalBuildResponse**
      }

      @Test
      public void test(){
          Map<String, Long> req = new HashMap<>();
          req.put("bac" , 10L);
          req.put("aaa" , 100L);
          impl.sendResponse(req)
      }
}

我想知道如何在PrepareResponse类中调用抽象方法finalBuildResponse()的实际实现吗?

0 个答案:

没有答案