Side effects between test cases
| Newsgroups | gmane.comp.java.junit.user |
|---|---|
| Message-ID | <[email protected]> |
Hi, I wanted to discuss two side effects between multiple JUnit test methods: a) timing issues If a test method runs into a timeout, the executing thread is interrupted (but not stopped). Hence, if it executes an infinite loop, it will still occupy some of your resources after this test failed. For an example, see [1]. The helper method is executed either once or infinite often in a loop. If executed once, it runs approx. 100ms on my machine. If executed in a loop, JUnit's runtime is approx. 600ms times for a single parameterized run. Executing 10 infinite loops first and then 10 single helper calls results in more than 10 timeouts (which is set to 500ms). You may have to increase NUM_RUNS on your machine to reproduce. b) memory issues It's easy to construct cases from the above insight (threads may keep running in case of a timeout) with side effects on memory which lead to an OutOfMemoryError. Even when threads do not keep running, and one test allocates many objects, the garbage collector may kick in during the execution of the next test method. This may result in an timeout that is not caused by the code under test but by the previous massive object allocation. I know that's almost impossible to stop Java threads. I also assume that both side effects are well known. So what's the best practice to reduce or even avoid these side effects when writing JUnit test methods? Cheers, Tobi -- 1: https://gist.github.com/meisterT/97db378abea366036f36 ------------------------------------ Posted by: Tobias Werth <[email protected]> ------------------------------------