assert object!= null和Assert.assertNotNull(object)之间有什么区别?

Wal*_*eed 10 java testing testng

下面两段代码有什么区别?

@Test  
public void getObjectTest() throws Exception {
    Object object;
    //Some code
    Assert.assertNotNull(object);
}
Run Code Online (Sandbox Code Playgroud)

@Test  
public void getObjectTest() throws Exception {
    Object object;
    //Some code
    assert object!=null;
}
Run Code Online (Sandbox Code Playgroud)

我确实理解这Assert.AssertNotNull是来自TestNG的函数调用,并且assert是Java的一个关键词(在Java 1.4中引入).这两者有其他差异吗?例如工作,表现等

vik*_*eve 10

那么第一种情况是使用测试框架中的Assert类,并且是正确的方法,因为TestNG将以智能方式报告错误.

您还可以将自己的消息添加到测试中:

Assert.assertNotNull(object, "This object should not be null");
Run Code Online (Sandbox Code Playgroud)

第二种情况使用assert关键字 - 它会给你一个失败,但堆栈跟踪可能一目了然也可能是不可理解的.您还需要注意,可能无法启用断言.