4

I had a failing unit test and wondered why. I found out that the test is executed 400 times. Trying to figure out what causes the test to execute 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); // comment this to run only once } } 

In my tearDown() method, I'm trying to find out the result of the tests, because my cleanup operation could takes some minutes, so I only want to clean up in case of a failure, but not in case of succcess.

I found the use of testResult() in code of the verifier rule and thought it would be useful for me, but it won't be useful if my tests are run 400 times as often.

Is that a bug of the experimental PrintableResult, am I doing something wrong or is there another/easier way of achieving my intended result?

7
  • 1
    Nice minimal test-case. However, on a side-note, I'm not entirely clear I understand the use-case here. What kind of cleanup do you need to do that you can happily skip in some cases but not others? Commented Nov 28, 2014 at 12:23
  • @OliverCharlesworth: If the test was not successful, I need to reset a virtual machine to a snapshot, which takes 5 minutes. I'm misusing JUnit to implement system tests. The tearDown() method shall become part of a base class. Commented Nov 28, 2014 at 12:26
  • 2
    See github.com/pomkine/junit/blob/master/src/main/java/org/junit/… Inside testResult it seems that your test executed again so it is recursion call. Commented Nov 28, 2014 at 13:08
  • Could you implement your own listener? junit.org/javadoc/latest/org/junit/runner/notification/… Commented Nov 28, 2014 at 13:20
  • @ pomkine : that should be the answer. Commented Nov 28, 2014 at 13:21

1 Answer 1

2

See Junit PrintableResult source Inside testResult it seems that your test executed again so it is recursion call. Some usefull info about getting test results can be found here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.