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

32 comments on “JUnit – Basic annotation examples

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

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

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

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

      Reply
  4. 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.

    Reply
  5.  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());
    	}
    }
     
    Reply
    1. 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

      Reply
  6. 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?

    Reply
    1. 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) 🙂

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

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

    Reply
  9. 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

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

    Reply
  11. Excellent web site…
    thinks for efforts and sharing knowledge…

    Java For Ever…

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *