JUnit 4 Tutorial 1 – Basic usage
Published: May 17, 2009 , Updated: April 1, 2010 , Author: mkyong
This tutorial introduces the basic annotation supported in Junit 4.
import org.junit.*; import static org.junit.Assert.*; import java.util.*; /** * @author mkyong * */ public class JunitTest1 { private Collection collection; @BeforeClass public static void oneTimeSetUp() { // one-time initialization code System.out.println("@BeforeClass - oneTimeSetUp"); } @AfterClass public static void oneTimeTearDown() { // one-time cleanup code System.out.println("@AfterClass - oneTimeTearDown"); } @Before public void setUp() { collection = new ArrayList(); System.out.println("@Before - setUp"); } @After public void tearDown() { collection.clear(); System.out.println("@After - tearDown"); } @Test public void testEmptyCollection() { assertTrue(collection.isEmpty()); System.out.println("@Test - testEmptyCollection"); } @Test public void testOneItemCollection() { collection.add("itemA"); assertEquals(1, collection.size()); System.out.println("@Test - testOneItemCollection"); } }
Result
@BeforeClass - oneTimeSetUp @Before - setUp @Test - testEmptyCollection @After - tearDown @Before - setUp @Test - testOneItemCollection @After - tearDown @AfterClass - oneTimeTearDown
In JUnit 4, you have to declare “@BeforeClass” and “@AfterClass” method as static method.
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
This site is really very helpful for the developers.
This web site is helping me a lot in daily live works
Thanks a lot for the stuff
it will be useful if you post some examples for the AJAX
Very good one for a beginner to go with.Thanks a lot for the stuff.
This website supported me so lot…thanks
Excellent web site…
thinks for efforts and sharing knowledge…
Java For Ever…
Yeah cool.nice one sir
Hi, good information sir. I will continue to visit your site. Conggrat.you have one more followers now
[...] Tutorial 1 – Basic usage Introduces basic annotation supported in Junit 4. [...]