I am writing unit tests and I'm stuck with a method which has infinite loop.
I have a main class which uses main
method for execution and hence i have to use junit.framework.Test
which does not have annotation such as @Test(timeout=1000)
, instead it calls the main class by creating its constructor.
Now the issue is : I have written all my test cases and its probably working fine too seeing the output but the test is stuck in an infinite loop.
From my main test class which is using junit.framework.Test
instead of org.junit.Test
. I called a public method of another class which has something like
public class AnotherClass{
public void method()
{
for( ; ;) // this is an infinite loop
{
.....
}
}
}
This is because it is actually monitoring a directory continuously and the program has to keep running until terminated manually.
My main test class calls this class's method and it never returns control to my test class again. Since, I'm writing tests, i cannot modify that method.
How do I terminate or take back control from that class(back to my test class)?
I tried using timer/thread in my test class to stop it from execution after certain time, but that does not work because the control goes over to the other class whose public method is called and its stuck there forever.
Any ideas on how to do this?
Please let me know, if you need any more info on this. Thanks
Note: The class which has infinite loop is not a test class, its a main class in package, so I cannot make any modifications there.
I have a solution which is kind of based on this logic but I'm hoping to find something better than this or may be finding what other solutions are possible for this.