Main Tutorials

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 Author

author image
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

Subscribe
Notify of
32 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Zhu Liang
8 years ago

Good for beginners.

LaiLa
9 years ago

thanks a lot

Yilhyeong Mun
9 years ago

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

jeff alice
10 years ago

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
7 years ago
Reply to  jeff alice

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

Raja
10 years ago

Thanks for these information………

bgt
10 years ago

Thanks alot for these tutorials. They helped me alot.

Valeriy
11 years ago

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.

Shrikant Kale
11 years ago
Ronald
10 years ago
Reply to  Shrikant Kale

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
11 years ago

You are awesome man..

Yury Kozarovitsky
11 years ago

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
10 years ago

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
7 years ago
Reply to  Van

Good to know this, thanks.

GG
11 years ago

ur website is very much dedicated to ppl learning java

Amit
11 years ago
Reply to  GG

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

Mallika
11 years ago

excellent information ..Thanks

Venkat
11 years ago
Reply to  Mallika

perfect

taqin21
11 years ago

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

javaboy
11 years ago

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

Mazhar
12 years ago

This site is really very helpful for the developers.

vishal
12 years ago

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
12 years ago

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

dragonvn
12 years ago

This website supported me so lot…thanks

Jazid
12 years ago

Excellent web site…
thinks for efforts and sharing knowledge…

Java For Ever…

Alex Sales
13 years ago

Yeah cool.nice one sir

Margery Ehrhardt
13 years ago

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

mist42nz
6 years ago

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

Majd Addin
11 years ago

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

Student
11 years ago
Reply to  Majd Addin

Sorry for the spam. Someone was on my computer.

Student
11 years ago
Reply to  Student

Please delete the comment.

meryem
11 years ago
 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
	public void setUp() throws Exception {
		record = new Record("RIEN","test.doc","C:\\Documents and Settings\\test.doc","2012-10-11-01-25-38","2011-10-11-01-25-38",128,130,"F");	    
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		record=null;
	}

	@Test	
	public void record() {	
		assertNotNull(record);	
	}
	
	@Test	
	public void record1() {	
		record1=new Record("C:\\Documents and Settings\\test.doc");
		assertNotNull(record1);	
		record1=null;
	}
	
	@Test	
	public void getStatut() {
		assertEquals("RIEN", record.getStatut());		
	}
	
	@Test
	public void setStatut() {
	record.setStatut("SUPP");
	assertEquals("SUPP", record.getStatut());
	}
	
	@Test	
	public void getName() {
		assertEquals("test.doc", record.getName());		
	}
	
	@Test
	public void setName() {
	record.setName("test1.doc");
	assertEquals("test1.doc", record.getName());
	}
	
	@Test	
	public void getChemin() {
		assertEquals("C:\\Documents and Settings\\test.doc", record.getChemin());		
	}
	
	@Test
	public void setChemin() {
	record.setChemin("C:\\Documents and Settings\\test1.doc");
	assertEquals("C:\\Documents and Settings\\test1.doc", record.getChemin());
	}
	
	@Test	
	public void getDateModification() {
		assertEquals("2012-10-11-01-25-38", record.getDateModification());		
	}
	
	@Test
	public void setDateModification() {
	record.setDateModification("2012-10-11-01-00-00");
	assertEquals("2012-10-11-01-00-00", record.getDateModification());
	}
	
	@Test	
	public void getAncienneDateModification() {
		assertEquals("2011-10-11-01-25-38", record.getAncienneDateModification());		
	}
	
	@Test
	public void setAncienneDateModification() {
	record.setAncienneDateModification("2010-11-11-01-25-38");
	assertEquals("2010-11-11-01-25-38", record.getAncienneDateModification());
	}
	
	@Test	
	public void getTaille() {
		assertEquals(128, record.getTaille());		
	}
	
	@Test
	public void setTaille() {
	record.setTaille(150);
	assertEquals(150, record.getTaille());
	}
	
	@Test	
	public void getAncienneTaille() {
		assertEquals(130, record.getAncienneTaille());		
	}
	
	@Test
	public void setAncienneTaille() {
	record.setAncienneTaille(110);
	assertEquals(110, record.getAncienneTaille());
	}
	
	@Test	
	public void getType () {
		assertEquals("F", record.getType());		
	}
	
	@Test
	public void setAType() {
	record.setType("R");
	assertEquals("R", record.getType());
	}
	
	@Test
	public void ToString() {
	assertEquals("RIEN;test.doc;C:"+File.separator+"Documents and Settings"+File.separator+"test.doc;2012-10-11-01-25-38;2011-10-11-01-25-38;128;130;F", record.ToString());
	}
}