How to test a method which has void return type and no arguments?

M

mohit.khatri28

Hi All,

I am using JUnit for testing java application under Eclipse IDE. I
have a method which has void return type and no arguments i.e.

void xyz()
{
..............
}

Could anybody suggest me how do i test this type of method using
JUnit.

Thanks & Regards
Mohit
 
E

Eric Sosman

Hi All,

I am using JUnit for testing java application under Eclipse IDE. I
have a method which has void return type and no arguments i.e.

void xyz()
{
..............
}

Could anybody suggest me how do i test this type of method using
JUnit.

Such a method only makes sense if it makes changes that
are observable through other means -- java.util.Set#clear(),
for example, takes no arguments and returns no value, but
changes the state of a Set in ways other methods can observe.

The way to test clear() is not to observe its behavior
directly, but to observe what happens to the Set when you
use it. A plausible unit test might apply clear() to Sets
in various states -- brand-new, with one element, with a
hundred elements, just-cleared, after insertion and individual
removal of elements -- and then use Set's other methods to see
whether clear() has had its intended effect: call isEmpty(),
or call size(), or call iterator().hasNext(), and so on.

Test your xyz() the same way. Figure out what effect it
is supposed to have in various circumstances, and use other
methods to verify that the effect has in fact occurred. If
xyz() is supposed to throw an exception in some cases (as
clear() is), that's another thing you can check.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top