无法断言AssertJ的异常

时间:2018-10-11 06:31:24

标签: spring junit junit4 assertj

我有以下负面测试代码(需要失败并引发异常):

@Test
public void testInventoryControllerGetNonExistingRegion() {

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

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

该测试调用以下方法get(),并且应引发EntityDoesNotExistException异常。

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

get()方法会根据需要抛出异常(我设置了一个断点并进行了验证)! 但是AssertJ向我显示了一条错误消息,但测试失败。

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

为什么会这样?

谢谢!

0 个答案:

没有答案