JUnit – Basic annotation examples
Here’re some basic JUnit annotations you should understand:
- @BeforeClass – Run once before any of the test methods in the class,
public static void
- @AfterClass – Run once after all the tests in the class have been run,
public static void
- @Before – Run before @Test,
public void
- @After – Run after @Test,
public void
- @Test – This is the test method to run,
public void
P.S Tested with JUnit 4.12
BasicAnnotationTest.java
package com.mkyong;
import org.junit.*;
public class BasicAnnotationTest {
// Run once, e.g. Database connection, connection pool
@BeforeClass
public static void runOnceBeforeClass() {
System.out.println("@BeforeClass - runOnceBeforeClass");
}
// Run once, e.g close connection, cleanup
@AfterClass
public static void runOnceAfterClass() {
System.out.println("@AfterClass - runOnceAfterClass");
}
// Should rename to @BeforeTestMethod
// e.g. Creating an similar object and share for all @Test
@Before
public void runBeforeTestMethod() {
System.out.println("@Before - runBeforeTestMethod");
}
// Should rename to @AfterTestMethod
@After
public void runAfterTestMethod() {
System.out.println("@After - runAfterTestMethod");
}
@Test
public void test_method_1() {
System.out.println("@Test - test_method_1");
}
@Test
public void test_method_2() {
System.out.println("@Test - test_method_2");
}
}
Output
@BeforeClass - runOnceBeforeClass
@Before - runBeforeTestMethod
@Test - test_method_1
@After - runAfterTestMethod
@Before - runBeforeTestMethod
@Test - test_method_2
@After - runAfterTestMethod
@AfterClass - runOnceAfterClass
Good for beginners.
thanks a lot
Thanks a lot for your post^^. These days, I am studying about Junit!
not sure what it does but the output look meaningless to me
System.out.println(“@Test – testOneItemCollection”);
this just print out and shows what ?
Just a testing message to show the order of the execution (when the declared annotation will be fired)
Thanks for these information………
Thanks alot for these tutorials. They helped me alot.
Thank you for these posts about Java.
Such a small examples are such a huge help for people, who haven’t deal with all this stuff.
Readers Should have a look at this.
https://www.cs.washington.edu/education/courses/143/11wi/eclipse-tutorial/junit.shtml
Shrikant, thanks a lot buddy, that saved my A**,, none of the internet explanation was absolute. This one was outstanding.
Thanks a ton for identifying it.
Regards,
Ronald
You are awesome man..
The @BeforeClass is not a one time setup. It runs once for every test class.
Do you know how to implement one time setup and teardown for multiple classes?
I know it’s old post but I just read this now. You could use a test suite and do setup on the first class(especially on it’s @BeforeClass) and teardown on the last class(especially on @AfterClass) 🙂
Good to know this, thanks.
ur website is very much dedicated to ppl learning java
You are a nice guy ..God bless you brother..
excellent information ..Thanks
perfect
thx u so much … gan .. i want to learned any tutorial in here …
Good Bless For U gan ..
good job man.
excellent work and information for java developers.
thanks
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 🙂
wouldn’t it run more effectively with some asserts or are they optional??
Trueeeeeeeee yaaaaaa 2chainz!!! #mkpres-a.k.a. swag master
Sorry for the spam. Someone was on my computer.
Please delete the comment.