- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
重构时Rultor使用Cactoos而不是 Guava ,我对 GithubProfileTest
的负面测试有疑问和 GithubProfileValidationTest
。
重构后,两个提到的测试类的正面测试用例都通过了,但是期望特定异常的负面测试用例失败了。被测受影响的重构代码为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 throw 'java.io.UncheckedIOException' exception
,而 Profile.ConfigException
只是忽略/隐藏。
MapOf
似乎无法评估或“隐藏”调用 this.asset 方法引发的异常,从而引发了 UncheckedIOException
,所以我我无法修复此问题并引发了 Profile.ConfigException
。
调试时,UncheckedIOException
不包含引发 Profile.ConfigException
的任何信息。
关于我为什么会出现这种行为或可能的解决方案的任何提示?
最佳答案
问题是 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);
}
关于java - 抛出 UncheckedIOException 而不是不同的预期异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46062515/
我想生成签名的apk文件时遇到此问题 来自构建输出栏 org.gradle.api.UncheckedIOException: Failed to capture snapshot of input
我的 Android Studio 项目工作正常,重启项目后我得到这个错误: Caused by: org.gradle.api.UncheckedIOException: Failed to cre
重构时Rultor使用Cactoos而不是 Guava ,我对 GithubProfileTest 的负面测试有疑问和 GithubProfileValidationTest。 重构后,两个提到的测试
编辑:这似乎不可能,请参阅 https://bugs.openjdk.java.net/browse/JDK-8039910 . 我有一个辅助类,它提供了一个 Stream .这段代码只是包装了 Fi
Java 8 正式介绍 java.io.UncheckedIOException到带有 Stream API 的 lambda 的 JDK 类库,因为 lambda 表达式不能声明它的 throws-
这个问题在这里已经有了答案: Files.readAllBytes vs Files.lines getting MalformedInputException (4 个答案) 关闭 6 年前。 我
我在安装最新的 android studio 预览版后尝试再次构建我的项目,但在更新 gradle 版本时出现以下异常: org.gradle.api.UncheckedIOException: Fa
嗨,我收到此错误,我无法弄清楚问题出在哪里,因为它之前一直在工作。当我尝试将快照jar上传到SonaType快照存储库时,将发生这种情况。 org.gradle.api.tasks.TaskExecu
我是一名优秀的程序员,十分优秀!