报告具有多个异常的 JUnit 测试方法的 throws 子句。 此类子句会造成不必要的冗余。 不会从其他项目代码调用测试方法,因此无需单独处理这些异常。

示例:


  @Test
  public void testReflection() throws NoSuchMethodException,
      InvocationTargetException, IllegalAccessException {
    String result = (String) String.class.getMethod("trim")
        .invoke(" hello ");
    assertEquals("hello", result);
  }

通过快速修复将异常声明替换为单个异常:


  @Test
  public void testReflection() throws Exception {
    String result = (String) String.class.getMethod("trim")
        .invoke(" hello ");
    assertEquals("hello", result);
  }