Re: Need help designing some JUnit tests
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".
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.
Patricia