Junit convention - assert
Is it convention (or just smarter) to limit a test method to one assert statement? or is is more than one ok?, e.g. (stupid example)
publicvoid testSomeObjects()
{
MyClass myclass =new MyClass();
MyClass myclass2 =new MyClass();
assertNotSame("Objects are the same",
myclass2,
myclass);
assertNotSame("Objects are the same",
myclass,
myclass);
}
I would limit tests to one method call and use whatever asserts are needed to validate that the method did what was anticipated.
For example if a method returns a class that is an implementation of a linked list of objects. First of all we want to make sure that the method call did not return null when it wasn't supposed to, we might want to assert that the object returned is of the correct type and that the list contains the number of the right type of objects that we were anticipating. So you might very easily have
List list = someMethod(Map someparameters) ;
assertNotNull(list) ;
assertTrue(list instanceof SpiffyClass) ;
assertTrue(list.size() == n) ;
Iterator i = list.iterator() ;
while(i.hasNext()){
assertTrue(iterator.next() instanceof OtherSpiffyClass) ;
}
Just an example of what I might or might not do depending on the situation. What I would not do is test for things that are not directly related, or for more than one method.
Just my 2 krupplenicks worth, your milage may of course vary.
PS.