0

I am trying to do a exception testcase in junit using AsssertJ. But I am getting the following error : Results :

Failed tests: BatchManagerTests.testUniqueBatchpart:225 Expecting code to raise a throwable.

Tests run: 149, Failures: 1, Errors: 0, Skipped: 0


The code for testcase is

@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");


    }

The code I am trying to test is :

 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;
    }
sromit
  • 900
  • 3
  • 16
  • 43

1 Answers1

0

assertThatThrownBy will fail if the given lambda does not throw an exception, in your case () -> batchManager.insertBatchParts(batchPartViewNext, userName) should have thrown a ValidationError but it apparently did not.

Joel Costigliola
  • 6,308
  • 27
  • 35