java - 如何获取 Eclipse 空分析以使用 JUnit 中的 assertNotNull
问题描述
如何让 eclipses null 分析与 jUnit 5 中的 assertNotNull 一起使用。在下面的程序中,我收到“Potientiel null 警告”,尽管由于上面的 assertNotNull 行,leaf 不可能为 null。
如果我将 assertNotNull 更改为 assert(leaf!=null) 警告就会消失。
根据这个(旧)链接,eclipse应该支持使用junit断言,并且我启用了“启用基于注释的空分析”
https://bugs.eclipse.org/bugs/show_bug.cgi?id=382069
LeafNode leaf=getLeafMayBeNull(); assertNotNull(leaf);
assertEquals(Long.valueOf(42),leaf.getLong());
解决方案
事实上,对于 JUnit 4,afterassertNotNull(o)
o
不能null
被硬编码的事实(在提到的 bug中跟踪并通过这个 commit 实现)。但是对于 JUnit 5,这还没有完成(参见org.eclipse.jdt.internal.compiler.lookup.TypeConstants
常量 fororg.junit.Assert
但没有常量 for org.junit.jupiter.api.Assertions
)。请将此报告给 Eclipse。
作为解决方法,您可以使用以下实用程序方法来避免潜在的空指针访问问题:
static <T> T notNull(@Nullable T o) {
assertNotNull(o);
if (o == null) throw new RuntimeException();
return o;
}
使用此实用程序方法,给定的代码片段如下所示:
LeafNode leaf = notNull(getLeafMayBeNull());
assertEquals(Long.valueOf(42),leaf.getLong());
推荐阅读
- python - 如果我预测一些显示服务器工作负载的数据,ARIMA 甚至没有接近预测一个类似的模式
- c# - 在 .NET Framework 中使用 Serilog 立即发送电子邮件通知
- r - missForest 需要多长时间才能为我的数据集估算数据 (n=7896)
- angular - 在 Angular 模块的工厂中注入和使用服务
- swiftui - SwiftUI - 具有全屏模式的 PresentationButton
- sql - Spring Data JPA:CriteriaQuery 为每个唯一外键获取具有最大值的实体
- .htaccess - 如何使用 TYPO3 v9 重定向旧文件 url
- vb.net - 表单加载时“不支持给定路径的格式”,
- joomla - 更新Joomla3.9项目中不同环境的数据库
- arrays - Vuejs:如何创建一个观察项目列表的计算属性?