抛出 UncheckedIOException 而不是不同的预期异常

UncheckedIOException is thrown instead of a different expected exception

重构 Rultor to use Cactoos instead of Guava, I’m having an issue with negative tests of GithubProfileTestGithubProfileValidationTest

重构后,正面测试用例通过了两个提到的测试类,但预期特定异常的负面测试用例失败了。 被测受影响的重构代码是 GithubProfile.assets 方法和 GithubProfile.asset 方法。

我将 assets 方法重构为如下所示:

 public Map<String, InputStream> assets() throws IOException {
    final XML xml = this.read();
    final List<XML> nodes = xml.nodes("/p/entry[@key='assets']/entry");
    return new MapOf<>(
        new Mapped<>(
            nodes,
            input ->
                new MapEntry<>(
                    input.xpath("@key").get(0),
                    this.asset(input.xpath("text()").get(0))
                )
        )
    );
}

在不同的测试用例中,this.asset 调用预计会抛出 Profile.ConfigException。相反,在调用 assets 方法时,测试失败并显示 Unable to evaluate the expression Method threw 'java.io.UncheckedIOException' exception,而 Profile.ConfigException 只是 ignored/hidden.

似乎 MapOf 以某种方式无法评估,或者 "hides",调用 this.asset 方法引发的异常,引发了 UncheckedIOException,所以我无法修复此问题并提出 Profile.ConfigException

调试时,UncheckedIOException 不包含引发 Profile.ConfigException 的任何信息。

关于我为什么会出现这种行为或可能的解决方案的任何提示?

原因可能是在 org.cactoos.func.UncheckedFunc 中迭代填充地图时完成的转换。

由于函数式编程通常不能很好地处理异常,API 试图避免声明已检查的异常。所以你可能不得不忍受它。

问题是 Iterable#next()(在 JDK 中)不允许抛出已检查的异常(如 Profile.ConfigException)。这就是为什么 org.cactoos.iterator.Mapped 捕获它们并抛出 UncheckedIOException 的原因。由于 JDK 设计,它无法修复。你能做的最好的事情就是老 for 循环:

public Map<String, InputStream> assets() throws IOException {
  final XML xml = this.read();
  final List<XML> nodes = xml.nodes("/p/entry[@key='assets']/entry");
  final List<MapEntry> entries = new LinkedList<>();
  for (final XML node : nodes) {
    entries.add(
      new MapEntry<>(
        input.xpath("@key").get(0),
        this.asset(input.xpath("text()").get(0)) // checked exeption here
      )
    );
  }
  return new MapOf<>(entries);
}