2

I have the following negative test code (needs to fail and throw an exception):

@Test
public void testInventoryControllerGetNonExistingRegion() {

    final String name = "ME_2";
    RegionConfiguration refRegionConfiguration = prepareReferenceObject(name);

    assertThatExceptionOfType(EntityDoesNotExistException.class).isThrownBy(() -> {
        inventoryRegionController.get(null,name);
    });
}

The test calls the following method get() and should throw an EntityDoesNotExistException exception.

@ApiOperation(value = "Get a region")
@RequestMapping(value = "regions/{" + REGION + "}", method = RequestMethod.GET)
public RegionConfiguration get(@RequestHeader(value = AUTHORIZATION_HEADER, defaultValue = "Bearer {{JWT}}") String authorization,
                                      @PathVariable(REGION) String regionName) throws EntityDoesNotExistException {

    InventoryRegionEntity inventoryRegionEntity = null;
    inventoryRegionEntity = RegionInventoryService.find(regionName);

    if(null != inventoryRegionEntity)
        return RegionConfigurationMapper.mapInventoryRegionEntity(inventoryRegionEntity);
    else
        throw new EntityDoesNotExistException(InventoryRegionEntity.class, regionName);
}

The get() method does throw the exception as required (I put a breakpoint and verified)! But AssertJ shows me an error message and fails my test.

java.lang.AssertionError: 
Expecting:
  <org.springframework.web.client.HttpClientErrorException: 404 null>
to be an instance of:
  <com.gms.contract.exception.EntityDoesNotExistException>
but was:
  <"org.springframework.web.client.HttpClientErrorException: 404 null

Why is that happening?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
dushkin
  • 1,939
  • 3
  • 37
  • 82
  • You had better testing the RegionInventoryService.find(regionName) first. To test the rest api, please refer to https://stackoverflow.com/questions/26237714/spring-junit-test-case-for-controller-rest-service – Miller Cy Chan Oct 15 '18 at 06:47
  • As the OP noted, the root of the problem lies elsewhere. Voted to close as "unable to reproduce". – Jongware Dec 24 '18 at 12:07

0 Answers0