以下面的伪代码为例,
Assert.assertNotNull(cut.func())
其中 cut
是被测试的非 null 类,func()
是返回 void 的函数。
此外,假设 func()
不会抛出任何错误,这将导致有效断言。
自从isn't possible to set an object to be null from within itself in Java / Kotlin ,(即func()
不能将cut
设置为null
),为什么在非null<内调用void函数是有效的/strong>断言?
仅仅是因为void
与null
不同吗?
最佳答案
Java 的 void
返回类型映射到 Unit
Kotlin 中的对象,且 Unit
不为 null
。
val x = System.out.println("") // void Java method
println(x == null) // "false"
println(x == Unit) // "true"
关于java - 为什么在 Assert.assertNotNull 中调用 void 函数是有效的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52054870/