Don't let test stop on failure

5.1k views Asked by At

I'm looking for the best practice for following (simplified) scenario:

@Test
public void someTest() {
  for(String someText : someTexts) {
    Assert.true(checkForValidity(someText));
  }
}

This test iterates through x-thousands of texts and in this case I don't want it to be stopped for each failure. I want the errors to be buffered and in case of error(s) to fail at the end. Has JUnit got something on board for for my aim?

1

There are 1 answers

2
kryger On BEST ANSWER

First of all, it's not really the correct way to implement this. JUnit allows parametrizing tests by defining a collection of inputs/outputs with the Parametrized test runner. Doing it this way ensures that each test case becomes a unique instance, making test report clearly state which samples passed and which ones failed.

If you still insist on doing it your way you should have a look at AssertJ's Soft Assertions which allow "swallowing" individual assertion failures, accumulating them and only reporting after the test is finished. The linked documentation section uses a nice example and is definitely worth reading.