JUnit – Basic annotation examples

Here’re some basic JUnit annotations you should understand:

  1. @BeforeClass – Run once before any of the test methods in the class, public static void
  2. @AfterClass – Run once after all the tests in the class have been run, public static void
  3. @Before – Run before @Test, public void
  4. @After – Run after @Test, public void
  5. @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

References

  1. JUnit @Before
  2. JUnit @BeforeClass

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
26 Comment threads
8 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
29 Comment authors
mist42nzmkyongZhu LiangLaiLaYilhyeong Mun Recent comment authors
newest oldest most voted
mist42nz
Guest
mist42nz

wouldn’t it run more effectively with some asserts or are they optional??

Zhu Liang
Guest
Zhu Liang

Good for beginners.

LaiLa
Guest
LaiLa

thanks a lot

Yilhyeong Mun
Guest
Yilhyeong Mun

Thanks a lot for your post^^. These days, I am studying about Junit!

jeff alice
Guest
jeff alice

not sure what it does but the output look meaningless to me
System.out.println(“@Test – testOneItemCollection”);
this just print out and shows what ?

mkyong
Guest
mkyong

Just a testing message to show the order of the execution (when the declared annotation will be fired)

Raja
Guest
Raja

Thanks for these information………

bgt
Guest
bgt

Thanks alot for these tutorials. They helped me alot.

Valeriy
Guest
Valeriy

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.

Majd Addin
Guest
Majd Addin

Trueeeeeeeee yaaaaaa 2chainz!!! #mkpres-a.k.a. swag master

Student
Guest
Student

Sorry for the spam. Someone was on my computer.

Student
Guest
Student

Please delete the comment.

meryem
Guest
meryem

package Tests; import static org.junit.Assert.*; import java.io.File; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT; import org.apache.log4j.Logger; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import source.Record; import tools.ClassManager; /** * @author matifi * */ public class RecordTest { /** * Instance du logger log4J */ private static final Logger logger = Logger.getLogger(RecordTest.class); private Record record,record1; /** * @throws java.lang.Exception */ @BeforeClass public static void setUpBeforeClass() throws Exception { logger.info("---------test de la classe Record : Début-----------"); } /** * @throws java.lang.Exception */ @AfterClass public static void tearDownAfterClass() throws Exception { logger.info("---------test de la classe Record : Fin-----------"); } /** * @throws java.lang.Exception */ @Before… Read more »

Shrikant Kale
Guest
Shrikant Kale
Ronald
Guest
Ronald

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

Abhishek
Guest
Abhishek

You are awesome man..

Yury Kozarovitsky
Guest
Yury Kozarovitsky

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?

Van
Guest
Van

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) :)

mkyong
Guest
mkyong

Good to know this, thanks.

GG
Guest
GG

ur website is very much dedicated to ppl learning java

Amit
Guest
Amit

You are a nice guy ..God bless you brother..

Mallika
Guest
Mallika

excellent information ..Thanks

Venkat
Guest
Venkat

perfect

taqin21
Guest
taqin21

thx u so much … gan .. i want to learned any tutorial in here …
Good Bless For U gan ..

javaboy
Guest
javaboy

good job man.
excellent work and information for java developers.
thanks

trackback
JUnit Tutorial « Zeeshan Akhter

[…] Tutorial 1 – Basic usage Introduces basic annotation supported in Junit 4. […]

Mazhar
Guest
Mazhar

This site is really very helpful for the developers.

vishal
Guest
vishal

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

Rajesh G
Guest
Rajesh G

Very good one for a beginner to go with.Thanks a lot for the stuff.

dragonvn
Guest
dragonvn

This website supported me so lot…thanks

Jazid
Guest
Jazid

Excellent web site…
thinks for efforts and sharing knowledge…

Java For Ever…

Alex Sales
Guest
Alex Sales

Yeah cool.nice one sir

Margery Ehrhardt
Guest
Margery Ehrhardt

Hi, good information sir. I will continue to visit your site. Conggrat.you have one more followers now :)

trackback
JUnit Tutorials | Tutorials

[…] Tutorial 1 – Basic usage Introduces basic annotation supported in Junit 4. […]