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?