This tutorial introduces how to write testcase with Junit 4 and explains basic annotations supported in junit 4.
Table 1. Annotations
Annotation | Description |
---|---|
@Test public void method() | The annotation @Test identifies that a method is a test method. |
@Before public void method() | Will execute the method before each test. This method can prepare the test environment (e.g. read input data, initialize the class). |
@After public void method() | Will execute the method after each test. This method can cleanup the test environment (e.g. delete temporary data, restore defaults). |
@BeforeClass public void method() | Will execute the method once, before the start of all tests. This can be used to perform time intensive activities, for example to connect to a database. Methods annotated with this annotation need a static modifier to work with JUnit. |
@AfterClass public void method() | Will execute the method once, after all tests have finished. This can be used to perform clean-up activities, for example to disconnect from a database. Methods annotated with @AfterClass need a static modifier to work with JUnit. |
@Ignore | Will ignore the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included. |
@Test (expected = Exception.class) | Fails, if the method does not throw the named exception. |
@Test(timeout=100) | Fails, if the method takes longer than 100 milliseconds. |
A class “Person” to be tested.
Person.java
package com.asjava; import java.util.Date; /** * Created by IntelliJ IDEA. * Time: 5:07:08 PM */ public class Person { private Date birthday; private String name; public Person(String name, Date birthday) { this.name = name; this.birthday = birthday; } public Person(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { if (this.birthday == null) return 0; Date now = new Date(); return now.getYear() - this.birthday.getYear() + 1; } }
The test code to test class “Person”:
PersonTest.java
package com.asjava; import org.junit.*; import java.util.Date; import java.text.SimpleDateFormat; /** * Created by IntelliJ IDEA. * Date: Aug 23, 2010 * Time: 5:13:25 PM */ public class PersonTest extends Assert { public static SimpleDateFormat df; private Person person; @BeforeClass public static void BeforeClass() { df = new SimpleDateFormat("yyyy-mm-dd"); System.out.println("BeforeClass"); } @Before public void setUp() throws Exception { Date date = df.parse("2005-10-14"); person = new Person("Jammy", date); System.out.println("setUp"); } @Test public void getName() { assertEquals(person.getName(), "Jammy"); System.out.println("Test getName"); } @Test public void getAge() { assertEquals(person.getAge(), 6); System.out.println("Test getAge"); } @After public void tearDown() throws Exception { person = null; System.out.println("tearDown"); } @AfterClass public static void AfterClass() { df = null; System.out.println("AfterClass"); } }
Output
BeforeClass setUp Test getAge tearDown setUp Test getName tearDown AfterClass
From the given output, the order of execution was:
BeforeClass>>setUp>Test getAge>>tearDown>>setUp>>Test getName>>tearDown>>AfterClass
Methods annotated with @BeforeClass
and @AfterClass
were only executed once, before the start of all tests or after the end of all tests, they are two new annotations introduced in junit 4. the disadvantage is that methods annotated with @beforeClass and @AfterClass need a static modifier.
Methods annotated with @Before and @After
were executed before/after each test, the method can prepare/cleanup the test environment (e.g. read input data, initialize the class).
Hi this is really helped me a lot….
Thanks for sharing info…across the globe….
Thank you so much….
Prakash
Very clear tutorial. Respect!
It will be great to see tutorials with EasyMock or JMock in the future
Thanks
Simple and Nice !