1

我正在测试 StringSearch 的算法,该算法中有许多 IllegalAccessExceptions。

我想测试代码在异常情况下是否正常工作......并且测试它们也很重要,因为它们在发生异常时调用了一些函数。

我只是想知道如何隐式生成异常。

我正在尝试使用 Java 安全管理器,但不知道如何使用它来撤销程序对 String 类的访问权限。

4

1 回答 1

1

尝试使用 Mockito 存根方法抛出异常

 doThrow(new RuntimeException()).when(mockedList).clear();

 //following throws RuntimeException:
 mockedList.clear();

http://gojko.net/2009/10/23/mockito-in-six-easy-examples/

http://mockito.googlecode.com/svn/tags/1.7/javadoc/org/mockito/internal/stubbing/Stubber.html

于 2012-12-09T02:02:18.347 回答