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.
The moose likes Testing and the fly likes Junit Test case for java.util.Timer/TimerTask Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Junit Test case for java.util.Timer/TimerTask" Watch "Junit Test case for java.util.Timer/TimerTask" New topic

Junit Test case for java.util.Timer/TimerTask

Alka Bhardwaj

Joined: Nov 27, 2007
Posts: 2
How can we create Junit test cases for class that schedules the task with Timer?
Timothy Frey
Ranch Hand

Joined: Jul 22, 2006
Posts: 56
You could just write test cases for your TimerTask; I think it's safe to assume that Timer works correctly.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
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.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Alka Bhardwaj

Joined: Nov 27, 2007
Posts: 2
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??
Timothy Frey
Ranch Hand

Joined: Jul 22, 2006
Posts: 56
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?
I agree. Here's the link:
subject: Junit Test case for java.util.Timer/TimerTask
It's not a secret anymore!