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. 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
Zhu Liang
Guest
Zhu Liang

Good for beginners.

trackback
JUnit Tutorials | Tutorials

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

Margery Ehrhardt
Guest
Margery Ehrhardt

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

Alex Sales
Guest
Alex Sales

Yeah cool.nice one sir

Jazid
Guest
Jazid

Excellent web site…
thinks for efforts and sharing knowledge…

Java For Ever…

dragonvn
Guest
dragonvn

This website supported me so lot…thanks

Rajesh G
Guest
Rajesh G

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

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

Mazhar
Guest
Mazhar

This site is really very helpful for the developers.

trackback
JUnit Tutorial « Zeeshan Akhter

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

javaboy
Guest
javaboy

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

taqin21
Guest
taqin21

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

Mallika
Guest
Mallika

excellent information ..Thanks

GG
Guest
GG

ur website is very much dedicated to ppl learning java

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?

Abhishek
Guest
Abhishek

You are awesome man..

Shrikant Kale
Guest
Shrikant Kale
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.

bgt
Guest
bgt

Thanks alot for these tutorials. They helped me alot.

Raja
Guest
Raja

Thanks for these information………

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 ?

Yilhyeong Mun
Guest
Yilhyeong Mun

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

LaiLa
Guest
LaiLa

thanks a lot

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 »

Majd Addin
Guest
Majd Addin

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

mist42nz
Guest
mist42nz

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