Re: Need help designing some JUnit tests
Patricia Shanahan <pats@acm.org> wrote in
news:UNqdndU3ZZ7MPGvWnZ2dnUVZ_h6dnZ2d@earthlink.com:
Rhino wrote:
Patricia Shanahan <pats@acm.org> wrote in
...
You seem to be assuming that a JUnit test requires an expected
result. Don't forget the assertTrue method, which lets you test
arbitrary conditions.
I'm not really familiar with assertTrue - I'm just getting back into
Java and JUnit after a gap of a few years and I was never all that
fluent with JUnit even before the gap. I'll look at the JUnit docs
and see what I can learn there about the purpose and best uses of
assertTrue....
I'll modify my suggestion from "Don't forget" to "Learn about".
I've already found a basic article on JUnit that talked about the
different sorts of assertXXX methods and looked at the JUnit API to get a
handle on that. It's much clearer now, thanks for the suggestion.
Thanks for the suggestion, I'm sure it will be helpful once I
understand it better ;-)
Here's a possibly relevant example, a method that tests an
Iterator<String> for non-empty, strictly increasing, no null elements.
/**
* Pass a non-empty String iterator with only non-null elements
* in strictly
* increasing order.
*/
private void testIteratorBehavior(Iterator<String> it) {
assertTrue(it.hasNext());
String oldElement = it.next();
assertNotNull(oldElement);
while (it.hasNext()) {
String newElement = it.next();
assertNotNull(newElement);
assertTrue(newElement.compareTo(oldElement) > 0);
oldElement = newElement;
}
}
Using this sort of technique you can test a structure for conforming
to some known rules without using an expected value.
Thanks you for the interesting suggestion. I hadn't thought of testing to
insure that the sequence was correct or that nulls were excluded and
might have struggled a bit trying to think of ways to ensure that.
Am I right in assuming that it is reasonable to bypass the sequence
testing in the case of my sorted list of Locales, given that I am using a
TreeMap which already guarantees the order? I assume you are just
including these tests to cover situations where I am generating a sorted
list out of thin air (without the benefit of the TreeXXX Collection
classes) so that I can be confident my code worked correctly?
I think I will include a test for nulls in the Set though, just in
case....
--
Rhino