覆盖java单元测试中的异常语句

Kau*_*mar 0 java testing testng unit-testing exception

如何为这段代码编写单元测试

   if (!isVisible()) {
       throw new IllegalStateException("Not in xyz page");
   }
   return this.isAttached(DOT_ID);
Run Code Online (Sandbox Code Playgroud)

如果isVisible()被模拟返回false那么如何编写异常语句的单元测试

Arn*_*lle 6

使用testng,您可以像这样注释您的测试方法:

@Test(expectedExceptions = IllegalStateException.class, 
      expectedExceptionsMessageRegExp = "...")
public void your_test() {
  [...]
}
Run Code Online (Sandbox Code Playgroud)