java assert object!=null 和 Assert.assertNotNull(object) 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16690242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between assert object!=null and Assert.assertNotNull(object)?
提问by Waleed
What is the difference between following two blocks of code?
以下两个代码块有什么区别?
@Test
public void getObjectTest() throws Exception {
Object object;
//Some code
Assert.assertNotNull(object);
}
AND
和
@Test
public void getObjectTest() throws Exception {
Object object;
//Some code
assert object!=null;
}
I do understand that Assert.AssertNotNull
is a function call from TestNG and assert
is a key word of Java ( introduced in Java 1.4 ). Are there any other differences in these two ? e.g. working, performance etc
我确实理解这Assert.AssertNotNull
是来自 TestNG 的函数调用,并且assert
是 Java 的关键字(在 Java 1.4 中引入)。这两者还有其他区别吗?例如工作、表现等
采纳答案by vikingsteve
Well the first case uses the Assertclass from your testing framework and is the right way to go, since TestNG will report the error in an intelligent way.
好吧,第一种情况使用测试框架中的Assert类,这是正确的方法,因为 TestNG 会以智能方式报告错误。
You can also add your own message to the test:
您还可以将自己的消息添加到测试中:
Assert.assertNotNull(object, "This object should not be null");
The second case uses the assert
keyword - it will give you a failure but the stack trace may or may not be understandable at a glance. You also need to be aware that assertions may NOT be enabled.
第二种情况使用assert
关键字 - 它会让您失败,但堆栈跟踪可能一目了然,也可能无法理解。您还需要注意,可能无法启用断言。
回答by Erik Pragt
Yes, there is: the assert keyword needs to be enabled using the -ea flag, like
是的,有:assert 关键字需要使用 -ea 标志启用,例如
java -ea MyClass
The assert can therefor be turned on and off without any changes to the code.
因此可以在不更改代码的情况下打开和关闭断言。
The Assert class will always work instead. So, if you're doing testing, use the Assert class, never the assert keyword. This might give you the idea all your tests are passing, and while actually they don't assert anything. In my 10+ years of coding, I've never seen anyone enable assertions except Jetbrains, so use Assert instead. Or better, use Hamcrest.
Assert 类将始终起作用。因此,如果您要进行测试,请使用 Assert 类,而不要使用 assert 关键字。这可能会让您认为您的所有测试都通过了,而实际上它们并没有断言任何内容。在我 10 多年的编码生涯中,除了 Jetbrains 之外,我从未见过任何人启用断言,因此请改用 Assert。或者更好的是,使用Hamcrest。
回答by Raheel
Talking about Performance:
谈性能:
assert object!=null;
is a single java statement but Assert.assertNotNull(object)
will result in calling of Multiple functions of TestNG, so w.r.t. performance assert object!=null;
will be slightly better.
assert object!=null;
是单个java语句,但是Assert.assertNotNull(object)
会导致调用TestNG的多个函数,所以wrt性能assert object!=null;
会稍微好一点。
回答by Adam Arold
The difference is basically the lack of assert
keyword.
This is the source from testng:
区别基本上是缺少assert
关键字。这是来自testng的来源:
static public void assertNotNull(Object object) {
assertNotNull(object, null);
}
static public void assertNotNull(Object object, String message) {
assertTrue(object != null, message);
}
Please note that the OP used the testng tag!
请注意,OP 使用了 testng 标签!
This is not JUnit's Assert
!
这不是 JUnit 的Assert
!
Some info about java's assert
keyword: assert
关于javaassert
关键字的一些信息: assert