This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Let's say you have three objects: - java.util.Timer - com.acme.YourTimerTask - com.acme.YourClassThatUsesTimer
You could test that... ...YourTimerTask does what it's supposed to when its run() method is invoked. ...YourClassThatUsesTimer schedules an appropriately configured YourTimerTask when it's supposed to.
Main class -> initialises log file, parameter from xml file, timer, schedules the task. Task -> Inturn calls log file to log the information, reads the parameter from the parameter file(which is initilized from the main class)
So the problem is that I am able to write the test case for Main class as it is the one that initilizes all the parameters and files. But when I try to run the test case for Task, half of the time it executes the exceptions as everything that it needs is not initilazed (as every configurable thing is indirectly available form Main class). I am not sure how to write a test case for the task. Its an old application which was written by someone else and project team wants to evaluavate if we can create unit test cases for each class. To me it looks like the testcase would look more like integrated test case than unit test case :-( Any suggestion if in such a case it is better to write normal integration test case or still can we write unit test cases??
Joined: Jul 22, 2006
Sounds like your TimerTask is strongly coupled with your main class. Is it possible for you to reduce this coupling. For example, instead of having your TimerTask access the member variables of your Main class directly, you can pass them into the TimerTask's constructor. Then you can provide prepared objects during your test cases:
In your production code, you would pass in the Main class's Log and paramter but during testing you can pass in whatever you want. Will this work in your case?