RandomizedTesting 是一个 JUnit 测试的扩展,封装了内置重复随机的测试和超时控制、参数工厂、线程执行组等等。
示例代码:
@ThreadLeakScope(Scope.TEST) @ThreadLeakAction({Action.WARN, Action.INTERRUPT}) @ThreadLeakLingering(linger =1000) publicstaticclassTheGoodBadAndUglyextendsRandomizedTest { @Test publicvoidgood() { // I do nothing and I'm good. } @Test @Repeat(iterations =10) publicvoidbad() { // I fail randomly, about 20% of the time. assertFalse(randomIntBetween(1,100) <=20); } @Test publicvoidugly() { // I start and abandon a thread which falls // outside the test's scope. The test will fail. newThread() { publicvoidrun() { RandomizedTest.sleep(5000); } }.start(); } }