Unit test questions

R

Rhino

Does anyone know of an online resource that suggests exactly how to do
unit tests on various kinds of methods? A resource that was focussed
particularly on Java would be especially useful.

I don't suppose it matters if the site is focussed particularly on JUnit,
although that it what I am using.

I am mostly interested in best practices for how to test various kinds of
methods and classes, particularly ones that may be a little unusual.

It's relatively self-evident how to unit-test a getter or setter, for
example, but not quite as self-evident how to test a constructor or a
hashcode method.

And in my case, I'd benefit from the advice of experts even on the types
of tests that seem relatively obvious. The ideas that I learned umpteen
years ago for how to test may have changed somewhat by now....

Basically, I'm looking for a website that would answer this kind of
question:
1. Given a setter method, is it sufficient to just attempt to set it to
one value, then that is within the range of its datatype, then use the
corresponding getter to make sure the value was set correctly or should a
proper test try to invoke the setter with values that are the wrong
datatype or out of range for the variable (e.g. if the setter is for a
short, suppy a value that is long or float to make sure that the right
things happen).
2. Given a getter method, is it sufficient to simply invoke it once and
see that it is the value that should be coming back? If not, how what
more should you do?
3. How do you test a constructor?
4. How do you test a method that has a void return?

I expect to have a bunch of other questions along these lines so, rather
than hopping on to this newsgroup for each of them as they come along,
I'd prefer to start with some principles.

I'm hoping the experts here can point me to something that is well-
regarded by the Java community.
 
M

markspace

Rhino said:
Does anyone know of an online resource that suggests exactly how to do
unit tests on various kinds of methods? A resource that was focussed
particularly on Java would be especially useful.


No websites, sorry. There are several books on the subject, however.
xUnit Test Patterns: Refactoring Test Code by Gerard Meszaros I *think*
specifically talks about Java but also includes generic patterns for all
types of languages. It might be worth looking at.

Regarding "how to test," one way to do it is to "white box" the method
(that is look inside to see how it works, as opposed to "black box"
which means you don't look at the implementation). Look at each branch
or if-statement in the code, and make sure your test exercises each
branch, both for the true and the false case. That way you at least
have "code coverage" for all code blocks, and no block goes untested.

So for a simple constructor like this

public MyObject( String value ) {
this.value = value;
}

You can just call with any old string and you've tested it. Whereas
with this:

public MyOtherObject( int value ) {
if( value < CONST1 || value > CONST2 ) {
throw new IllegalArgumentException();
}
this.value = value;
}

You want to make sure that you test three values, one on either side of
the valid range and one within the range. There are two blocks of code,
the bit that throws the exception, and the bit after that assigns the
value. Make sure each of the blocks is exercised and runs correctly.

Etc. Just keep looking for code branches and blocks, and try to
exercise each one, and your test will at least be decent. There are
other, more sophisticated tests, such as those test to ensure an API
complies with a specification, but that's a *compliance test*, not a
unit test. Unit tests should be pretty basic and just make sure that
nothing truly bad is happening. Leave the compliance tests to higher
level code.

For a hashcode:

public int hashcode() {
int hash = value;
hash = hash * 17 + othervalue;
hash = hash * 17 + morevalues;
return hash;
}

I don't see any branches there. Call it once and that's all you need,
imo. Again, super sophisticated tests are not the point of unit
testing. Stability is. You should add some tests for correctness
eventually, but those don't have to be super sophisticated, imo. If you
can at least prove you can run with out throwing errors, that's the
first step. Get that done first.
 
J

John B. Matthews

Rhino said:
Does anyone know of an online resource that suggests exactly how to do
unit tests on various kinds of methods? A resource that was focussed
particularly on Java would be especially useful.

Don't overlook the Cookbook and FAQ:

<http://junit.sourceforge.net/>

I just never get tired of "Where should I put my test files?"
 
A

Arne Vajhøj

Does anyone know of an online resource that suggests exactly how to do
unit tests on various kinds of methods? A resource that was focussed
particularly on Java would be especially useful.

I don't suppose it matters if the site is focussed particularly on JUnit,
although that it what I am using.

I am mostly interested in best practices for how to test various kinds of
methods and classes, particularly ones that may be a little unusual.

It's relatively self-evident how to unit-test a getter or setter, for
example, but not quite as self-evident how to test a constructor or a
hashcode method.

And in my case, I'd benefit from the advice of experts even on the types
of tests that seem relatively obvious. The ideas that I learned umpteen
years ago for how to test may have changed somewhat by now....

Basically, I'm looking for a website that would answer this kind of
question:
1. Given a setter method, is it sufficient to just attempt to set it to
one value, then that is within the range of its datatype, then use the
corresponding getter to make sure the value was set correctly or should a
proper test try to invoke the setter with values that are the wrong
datatype or out of range for the variable (e.g. if the setter is for a
short, suppy a value that is long or float to make sure that the right
things happen).
2. Given a getter method, is it sufficient to simply invoke it once and
see that it is the value that should be coming back? If not, how what
more should you do?
3. How do you test a constructor?
4. How do you test a method that has a void return?

I expect to have a bunch of other questions along these lines so, rather
than hopping on to this newsgroup for each of them as they come along,
I'd prefer to start with some principles.

I'm hoping the experts here can point me to something that is well-
regarded by the Java community.

Maybe you could find an open source package with a huge
number of unit tests associated and look at what they
have done.

Arne
 
T

Tom Anderson

Maybe you could find an open source package with a huge number of unit
tests associated and look at what they have done.

JUnit presumably has tests of its own. Those might be worth a look.

tom
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top