RandomizedTesting:

A short example

A super-short teaser

  public static class TheGoodBadAndUgly extends RandomizedTest {
    @Test
    public void good() {
      // I do nothing and I'm good.
    }
    
    @Test
    @Repeat(iterations = 10)
    public void bad() {
      // I fail randomly, about 20% of the time.
      assertFalse(randomIntBetween(1, 100) <= 20);
    }
    
    @Test
    public void ugly() {
      // I start and abandon a spinning thread outside the test scope.
      new Thread() {
        public void run() {
          while (true) {/* Spin. */}
        }
      }.start();
    }
  }

The above suite contains a simple passing test (good), 10 repetitions of a test named bad that uses pseudo-randomness to generate different execution paths every time (should any iteration fail, the exact same pseudo-random execution can be reproduced by providing the same initial seed), and a third test named ugly showing how the runner handles threads leaking outside of each test's execution boundary.

If executed in Eclipse, the above suite will result in the following (can you spot the random seed of the failed test?):

TheGoodBadAndUgly, Eclipse tests view

There is much more to discover in RandomizedTesting, check out the test-by-test tutorial examples at GitHub or in source code.