Java 在发生异常后评估资产?

Java 在发生异常后评估资产?,java,error-handling,testng,runtimeexception,Java,Error Handling,Testng,Runtimeexception,版本:testng-6.8.8.jar 此测试以绿色运行: @Test(expectedExceptions = { NullPointerException.class }) public void shouldTestNGIgnoreAssertsAfterExceptionThrown() throws Exception { String iAmNull = null; int length = iAmNull.length(); assertEquals(0, 1); }

版本:testng-6.8.8.jar

此测试以绿色运行:

@Test(expectedExceptions = { NullPointerException.class })
public void shouldTestNGIgnoreAssertsAfterExceptionThrown() throws Exception {
  String iAmNull = null;
  int length = iAmNull.length();
  assertEquals(0, 1);
}
任何配置文件或其他选项

要在异常发生后继续并评估断言?

您必须重写测试。 例如:

@Test
public void shouldTestNGIgnoreAssertsAfterExceptionThrown() {
  String iAmNull = null;
  boolean hasNpe = false;
  try {
    int length = iAmNull.length();
  } catch(NullPointerException npe) {
    hasNpe = true;
  }
  assertTrue(hasNpe);
  assertEquals(0, 1);
}

它与TestNG无关。这是关于Java如何工作(以及应该如何工作)的。对null(
.someMethod()
)的任何方法调用都会引发NullPointerException。如果在方法中没有捕捉到它,它将被传播到调用堆栈中。如果在任何地方都没有处理,则执行将以堆栈跟踪结束。

TestNG无法知道在异常之后还有其他断言。在这种情况下,您必须使用try/catch。