1. Overview
In this post, we will discuss frequently used Annotations, which is available when you include junit4.jar in your Classpath.
The frequently used Annotations are:
Let's discuss each annotation with an example.
2. JUnit 4 Annotations with Examples
2.1 @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 :
@Before
public void setUp() {
System.out.println("@Before method will execute before every JUnit4 test");
}
@After
public void tearDown() {
System.out.println("@After method will execute after every JUnit4 test");
}
2.2 @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
@BeforeClass
public static void setUpClass() throws Exception {
System.out.println("@BeforeClass method will be executed before JUnit test for"
+ "a Class starts");
}
@AfterClass
public static void tearDownClass() throws Exception {
System.out.println("@AfterClass method will be executed after JUnit test for"
+ "a Class Completed");
}
2.3 @Test
@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
@Test
public void testCalculateInterest() {
System.out.println("calculateInterest");
fail("An Example of @Test JUnit4 annotation");
}
2.4 @Ignore
Some time we add test method in JUnit test class but hasn't implemented that is causing your build to fail if JUnit test case is integrated or embedded into the 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:
@Ignore("Not yet implemented")
@Test
public void testGetAmount() {
System.out.println("getAmount");
fail("@Ignore method will not run by JUnit4");
}
2.5 @Test(timeout=500)
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.
@Test(timeout = 500)
public void testTimeout() {
System.out.println("@Test(timeout) can be used to enforce timeout in JUnit4 test case");
while (1 == 1) {
}
}
This JUnit4 test will fail after 500 milliseconds.
2.6 @Test(expected=IllegalArgumentException.class)
Another useful enhancement is Exception handling test cases 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 running with invalid input:
@Test(expected=IllegalArgumentException.class)
public void testException(int input) {
System.out.println("@Test(expected) will check for specified exception during its run");
}
3. Conclusion
In this post, we have seen a list of frequently used JUnit 4 annotations and their meanings. Also, we have seen the frequently used annotations like
@Before
@BeforeClass
@After
@AfterClass
@Test
@Ignore
@Test(timeout=500)
@Test(expected=IllegalArgumentException.class)
Read more on JUnit 4 Developers Guide.
Comments
Post a Comment
Leave Comment