怎么用jUnit模拟ModelMapper?

来源:爱站网时间:2021-11-16编辑:网友分享
爱站技术小编今天用一篇文章几分钟的时间教大家怎么用jUnit模拟ModelMapper,这篇文章实用性价值还挺高的,希望能给大家带来一定的帮助。

问题描述


我正在尝试进行一些UnitTest,并且我有一个这样的方法;

public void processInitialBalances(RequestDTO requestDTO) {
    logger.info("At processInitialBalances");

    // Convert from DTO to Entity
    Balance balance = modelMapper.map(requestDTO.getInitialBalances(), Balance.class);

    balance.setId(DUMMY_ID);
    Type listType = new TypeToken>() {}.getType();
    List issuer = modelMapper.map(requestDTO.getInitialBalances().getIssuers(), listType, "issuer-list");

    if (!issuer.isEmpty()) {
        issuer.get(ibcConstants.ZERO).setId(DUMMY_ID);

    balance.setIssuer(issuer);

    // Save balance entity and childs (issuers)
    balance = balanceRepository.save(balance);

    logger.info("Leaving processInitialBalances");
}

我正在努力在UnitTest中传递此行;

    Balance balance = modelMapper.map(requestDTO.getInitialBalances(), Balance.class);

    balance.setId(DUMMY_ID);
    Type listType = new TypeToken>() {}.getType();
    List issuer = modelMapper.map(requestDTO.getInitialBalances().getIssuers(), listType, "issuer-list");

因为当我运行classUnderTest.processInitialBalances(dto);我在line balance.setId(id);

处获得了NPE

我尝试模拟modelMapper,但出现一个PotentiallyStubbingProblem,这是我的测试。

    @Test
    public void testBalanceCreated() throws ServiceException {
        RequestDTO requestDTO = new RequestDTO();
        BalanceDTO balanceDTO = new BalanceDTO();
        List issuers = new ArrayList();
        IssuerDTO issuer = new IssuerDTO();

        issuer.setIssuerName("GBM");
        issuer.setSharePrice(10f);
        issuer.setTotalShares(100);
        issuers.add(issuer);

        balanceDTO.setCash(100f);
        balanceDTO.setIssuers(issuers);

        requestDTO.setInitialBalances(balanceDTO);

        when(modelMapper.map(null, null, null)).thenReturn(new Balance());
        when(modelMapper.map(null, null)).thenReturn(new ArrayList());

        cut.processInitialBalances(requestDTO);

        // Assert entities and repository call.
    }

任何建议将不胜感激。

思路:


when(modelMapper.map(isNull(), isNull(), isNull())).thenReturn(new Balance());
when(modelMapper.map(isNull(), isNull())).thenReturn(new ArrayList());

 以上内容就是爱站技术频道小编为大家分享的怎么用jUnit模拟ModelMapper?看完以上分享之后,大家应该都知道怎么操作了吧。

上一篇:怎么在Java中使用Diff-Match-Patch逐行比较两个字符串?

下一篇:怎么从AWS开发工具包STS更改AWS凭证配置文件默认文件路径?

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载