0

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.

Community
  • 1
  • 1
Polynomial Proton
  • 5,020
  • 20
  • 37
  • Use threads? Would that work? – EDToaster Jan 24 '15 at 04:38
  • I updated my question. I do kind of have a solution do get this done, but I'm waiting to find something else(or better) – Polynomial Proton Jan 24 '15 at 04:51
  • possible duplicate of [How to unit test a method that runs into an infinite loop for some input?](http://stackoverflow.com/questions/5959343/how-to-unit-test-a-method-that-runs-into-an-infinite-loop-for-some-input) – Joe Jan 24 '15 at 06:33
  • @Joe i already checked that question and it doesnt answer my question. Thanks for posting though. – Polynomial Proton Jan 24 '15 at 06:36
  • Why specifically can you not use JUnit4? – NamshubWriter Jan 25 '15 at 17:08
  • Also, what do you expect your test to verify about this method that never returns? Have you asked the person writing the method to break it up into smaller pieces for testability? – NamshubWriter Jan 25 '15 at 17:14
  • This is the first time i'm doing any unit testing. The main class has only one main method which takes an argument from where the program starts and hence using the test suite, since we cant test main method(my understanding) or may be the business logic is in a such way that we got to use test suite. Anyhow, i need to use test suite and I finally used `Thread` to get this done. – Polynomial Proton Jan 25 '15 at 18:40

1 Answers1

0

I think you really need to partition the problem into smaller parts and then combine the solutions together to get the final solution:

  1. you have a program that has infinite loop that you cannot change, but you need to test it
    • the only way to get the execution back to the test is by executing the logic in a separate thread
  2. You need to kill/stop the thread after that
    • in Java you cannot generally do that easily, you might consider using ExecutorThread and cancelling the Futures, maybe even daemon threads
  3. You need to have a timeout like behavior
    • easiest way is to use JUnit or TestNG build-in feature

The final solution will be the combination of all the partial solutions.

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40