I had an unsuccessful unit test and wondered why. I found out that the test runs 400 times. Trying to figure out what causes the test to run so many times, I reduced the code to the following:
package reproduce;
import org.junit.After;
import org.junit.Test;
import static org.junit.experimental.results.PrintableResult.testResult;
public class FailingTest
{
static int objCount = 0;
public FailingTest()
{
objCount++;
}
@Test
public void test()
{
System.out.println(objCount);
}
@After
public void tearDown()
{
testResult(FailingTest.class);
}
}
In my tearDown () method, I try to find out the test result, because my cleanup operation may take several minutes, so I want to clean up only in case of failure, but not in case of successful execution.
I found the use of the validation ruletestResult() in the code and thought it would be useful for me, but it would not be useful if my tests run 400 times more often.
Is this an experimental PrintableResult error, am I doing something wrong or is there another / simpler way to achieve my intended result?