首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AssertJ的测试失败

AssertJ的测试失败
EN

Stack Overflow用户
提问于 2017-02-03 15:47:09
回答 1查看 1.2K关注 0票数 0

我正在尝试使用AsssertJ在junit中执行异常测试。但我得到了以下错误:结果:

失败的测试: BatchManagerTests.testUniqueBatchpart:225期望代码引发可抛出。

测试运行: 149,失败: 1,错误: 0,跳过:0

测试用例的代码是

代码语言:javascript
复制
@Test
    public void testUniqueBatchpart(){
        String userName = "502689031";
        List<BatchPartView> batchPartViewList = new ArrayList();
        BatchPart batchPart = initBatchPart(new BatchPart(), 1L, 1L, 1L,  1L, false);
        BatchPart batchPartNext = initBatchPart(new BatchPart(), 2L, 1L, 1L,  2L, false);
        BatchPartView batchPartView = initBatchPartView(batchPart);     
        BatchPartView batchPartViewNext = initBatchPartView(batchPartNext);

        batchPartView = batchManager.insertBatchParts(batchPartView, userName);
        batchManager.insertBatchParts(batchPartViewNext, userName);

        assertThatThrownBy(() -> batchManager.insertBatchParts(batchPartViewNext, userName))
                            .isInstanceOf(ValidationError.class)
                            .hasMessage(" Unique constraint violation encountered");


    }

我要测试的代码是:

代码语言:javascript
复制
 public BatchPartView insertBatchParts(BatchPartView batchPartView, String userName) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("BatchManager:::insertBatchParts()");
        }
        Batch batch;
        BatchPartView returnBatchPartView = null;
        try {
            batch = batchRepository.findByMachineIdAndActiveTrue(batchPartView.getPart().getMachineId());
            Long falseCount = batchPartsRepository
                    .countByBatchIdInAndPartIdInAndDeletedFalse(batchPartView.getBatchId(), 
                            batchPartView.getPart().getId());

            if (null == batch) {
                batch = batchPartEngine.saveBatch(batchPartView, userName);
                returnBatchPartView = batchPartEngine.saveBatchPart(batchPartView, batch, userName);
            } else {
                if (falseCount < 1) {
                    returnBatchPartView = batchPartEngine.saveBatchPart(batchPartView, batch, userName);
                }
                else {
                    Set<BRSValidationError> errorSet = new HashSet<>();
                    errorSet.add(new BRSValidationError(ERROR, UNIQUECONSTRAINTVIOLATION));
                    if (!errorSet.isEmpty()) {
                        throw new ValidationError(errorSet);
                    }
                }
            }
        } catch (Exception ex) {
            LOGGER.error("", ex);
            Set<BRSValidationError> errorSet = new HashSet<>();
            errorSet.add(new BRSValidationError(ERROR, ex.getMessage()));
            if (!errorSet.isEmpty()) {
                throw new ValidationError(errorSet);
            }
        }
        return returnBatchPartView;
    }
EN

回答 1

Stack Overflow用户

发布于 2017-02-03 22:11:43

如果给定的lambda不抛出异常,assertThatThrownBy将失败,在您的示例中,() -> batchManager.insertBatchParts(batchPartViewNext, userName)本应抛出一个ValidationError,但显然没有。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42027997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档