代码之家  ›  专栏  ›  技术社区  ›  dushkin

无法用AssertJ断言异常

  •  2
  • dushkin  · 技术社区  · 6 年前

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

    @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 回复  |  直到 6 年前