Reputation: 7894
I have a JUnit test case where I'm expecting a particular method call to take a long time (over a minute). I want to
How do I do this?
Upvotes: 4
Views: 4600
Reputation: 10468
You can write a class implementing runnable that wraps around the method of interest; assuming spawning threads is allowed.
public class CallMethod implements Runnable
{
//time in milli
public long getStartTime()
{
return startTime;
}
//time in milli
public long getEndTime()
{
return endTime;
}
public void run()
{
startTime = ...;
obj.longRunningMethod();
endTime = ...;
}
}
Then in your JUnit you can do something like:
public void testCase1()
{
CallMethod call = new CallMethod();
Thread t = new Thread(call);
t.start();
t.join(60000); // wait one minute
assertTrue(t.alive() || call.getEndTime() - call.getStartTime() >= 60000);
}
Upvotes: 9