JUnit4 Annotations are single big change from JUnit 3 to JUnit 4 which is introduced in Java 5. With annotations creating and running a JUnit test becomes more easy and more readable, but you can only take full advantage of JUnit4 if you know the correct meaning of annotations used on this version and how to use them while writing tests. In this
Junit tutorial we will not only understand meaning of those annotations but also we will see examples of JUnit4 annotations. By the way this is my first post in unit testing but if you are new here than you may like post 10 tips to write better code comments and 10 Object oriented design principles for Programmer as well.
JUnit 4 Annotations : Overview
Following is a list of frequently used Annotations , which is available when you include junit4.jar in your Classpath:
@Before and @After
In Junit4 there is no setup() or tearDown() method and instead of that we have @Before and @After annotations.
By using @Before you can make any method as setup() and by using @After you can make any method as teardown(). What is most important point to remember is @Before and @After annotated method will be invoked before and after each test case. So in case you have five test cases in your JUnit test file than just like setup() and tearDown() method annotated with @Before and @After will be called five times. Here is an example of using
@Before and @After Annotation :
@BeforeClass and @AfterClass
@BeforeClass and @AfterClass JUnit4 Annotations are similar to @After and @Before with only exception that they
are called on per TestClass basis and not on per test basis. They can be used as one time setup and tearDown
method and can be used to initialize class level resources. here is an example of using @BeforeClass and @AfterClass Annotations in JUnit4, here is an example of @BeforeClass and @AfterClass Junit 4 annotation
@Test is a replacement of both TestCase class and convention "test" which we prefix to every test method. for example to test a method called calculateInterest() we used to create method testCalcuatedInterest() and our class needs to be extended from org.junit.TestCase class. Now with @Test annotation that is not required any more. You just need to annotate your test method with @Test Junit4 annotation and done. no need to extend from TestCase class and no need to prefix "test" to your method, here is an example of JUnit 4 @Test annotation
Some time we add test method in JUnit test class but hasn't implemented that is causing your build to fail if JUnit testcase are integrated or embedded into build process. you can avoid that problem by marking your test method as @Ignore in Junit4. JUnit4 ignores method annotated with @Ignore and doesn't run during test. Here is an example of using @Ignore annotation in JUnit4 to exclude a particular Test from running:
Now with JUnit4 writing testcases based on timeout is extremely easy. You just need to pass a parameter timeout with value in millisecond to @Test annotation. remember timeout values are specified in millisecond and your JUnit4 timeout test case will help if it doesn't complete before timeout period. This works great if you have SLA(Service Level Agreement) and an operation need to complete before predefined timeout.
This JUnit4 test will fail after 500 millisecond.
Another useful enhancement is Exception handling testcases of JUnit4. Now to test Exception is become very easy and you just need to specify Exception class inside @Test annotation to check whether a method throws a particular exception or not. here is an example which test behavior of a method to verify whether it throws Exception or not, when run with invalid input:
These were list of frequently used JUnit 4 annotations and there meanings. In the course we have also learn how to use @Before , @After in place of setup() and teardown(). Code review and Unit testing is one of the best development practices to follow and we must try our best to incorporate that in our daily coding and development cycle.
Recommended Java articles: