JUnit 4 - Test suites using annotations
- February 17th, 2011
- Posted in Computer . Programming
- Write comment
JUnit 4 provides annotation for testing. I had trouble finding how to bundle several test cases together in a test suite using that method. Finally, this article helped:
http://radio.javaranch.com/lasse/2006/07/27/1154024535662.html
It seems that not that many developers migrating to JUnit 4 are aware of the replacement for the old way of building suites--using the static suite() method and the junit.framework.TestSuite class--I decided to blog a simple example for Google's indexing hamsters to grab hold of.
Here we go:
import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ TestCalculatorAddition.class, TestCalculatorSubtraction.class, TestCalculatorMultiplication.class, TestCalculatorDivision.class }) public class CalculatorSuite { // the class remains completely empty, // being used only as a holder for the above annotations }The above class is a simple placeholder for the suite annotations, not containing any other functionality as such. The key is in the
@RunWithannotation, which tells the JUnit 4 test runner to use theorg.junit.runners.Suiteclass for running this particular class. The@Suiteannotation, on the other hand, tells the Suite runner which test classes to include in this suite and in which order.I hope this helps folks out there since it doesn't seem to be documented too well in junit.org.
update: there's an upcoming book - a 2nd edition of JUnit in Action that's going to cover JUnit 4. Its release is scheduled for June 2009 so there's still some time before the apparent documentation gap should be gone. Having said that, the book is on Manning's early access program (MEAP) so we should start seeing more and more chapters as we near the release date.
No comments yet.