September 5, 2015
Turning Non-Terminating JUnit Tests Into Failing TestsSo, I'm noodling with an implementation of a square root algorithm in Java, and after I get the main logic sort of working, methinks to meself "Oh, I should probably catch inputs that are negative numbers, because the algorithm won't converge in those cases".
So, I writes me a test to catch the expected exception I plan to throw, and then - being a fairly disciplined TDD sort of a dude - I run the test to see it fail. Low and behold, it does exactly what I expect - it doesn't converge. Which means it just goes around and around in the loop and JUnit hangs like this:
Now, having a unit test that doesn't terminate presents us with a problem. yes, technically, not terminating is a kind of failing, but if this is one test in a suite, then how do draw a line under it so we can move on to running other tests in the suite?
You can make the test fail using the optional timeout parameter of @Test. This is the simplest way to make the test actually fail.
NUnit has a similar attribute you can use. I imagine some of the other xUnit implementations do, too.
Posted 2 years, 11 months ago on September 5, 2015