I have a JUnit test class with a number of tests. To increase scenario coverage some data in our tests is randomized, meaning it may take a range of values between individual test runs, for example:
protected MonthlyAmountWithRemainder getMonetaryAmountMultipleOf(int multiplier) {
BigDecimal monthly = randomBigDecimal(1000);
BigDecimal multiply = new BigDecimal(multiplier);
BigDecimal total = monthly.multiply(multiply);
return new MonthlyAmountWithRemainder(total, monthly, ZERO);
}
Do you see this randomBigDecimal(1000)
? that may generate any value between 0 and 1000. We can also randomize date and some other values in the test.
Typically, our tests run just fine, but once in a blue moon (current scenario I'm talking about is once in about 50 times), a test fails without any apparent reason. As you can imagine, such rare failure makes it impossible to debug a test case to find out the reason for the failure or fix it.
So, the question is: is it possible to capture the data generated in the failed test run and re-run the test with exactly the same test data, so that I could debug the failing scenario? In other words, I would like to re-live my previous failed test run. Could that be achieved?