Hi All,
How i can test a method say X with JUnit which returns void.
This X method internally calls many other methods which are returnig
something.
Thanking You.
Akhilesh
Daniel Dyer - 16 Dec 2006 10:53 GMT
> Hi All,
>
> How i can test a method say X with JUnit which returns void.
> This X method internally calls many other methods which are returnig
> something.
What does the method do? Are there pre- and post-conditions (i.e. is
there some state that you can query before and after invoking it in order
to determine if it worked properly)?
Dan.

Signature
Daniel Dyer
http://www.uncommons.org
andrewmcdonagh - 16 Dec 2006 13:04 GMT
On Dec 16, 9:45 am, akhileshkokarde...@gmail.com wrote:
> Hi All,
>
[quoted text clipped - 5 lines]
>
> Akhilesh
Test its effect upon the rest of the system.
e.g.
Here's a clock class with Tick method that does not return anything....
import junit.framework.TestCase;
public class ClockTest extends TestCase {
public void testTickIncrementsTheTime() {
Clock clock = new Clock(0, 1);
clock.tick();
assertEquals("Wrong time!", "0:2", clock.getTime());
}
class Clock {
private int minutes;
private int seconds;
public Clock(int mins, int secs) {
minutes = mins;
seconds = secs;
}
public String getTime() {
return minutes + ":" + seconds;
}
public void tick() {
seconds++;
}
}
}
Andrew
John Ersatznom - 16 Dec 2006 22:11 GMT
> Hi All,
>
> How i can test a method say X with JUnit which returns void.
> This X method internally calls many other methods which are returnig
> something.
One would assume that it has a side effect. Test for the side effect.
E.g. if the method is a setter, call the getter afterward in your unit
test and check that it behaved appropriately. If it's something more
complicated, it will be trickier to manage, and may even require
checking a property sheet or disk file has been altered appropriately.
Richard Wheeldon - 17 Dec 2006 22:09 GMT
> How i can test a method say X with JUnit which returns void.
> This X method internally calls many other methods which are returnig
> something.
1. Test pre and post conditions.
2. Take a look at Mock Objects.
Richard