Start UnitTesting – Junit, The begining

Maven Dependency for Junit

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
</dependency>

Important Annotations

@BeforeClass – runs once before any test method in class
@AfterClass – runs once after all the tests in class have been run
@Before – runs before test method
@After – runs after the test method
@Test – This is test method to run.

Lets write a sample test class with all these Annotations

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class StratUnitTesting {

    public StratUnitTesting() {
    }

    @BeforeClass  // runs only once before the class
    public static void setUpClass() {
        System.out.println("inside @BeforeClass");
        System.out.println("=============================");
    }

    @AfterClass // runs onlyu once after the class
    public static void tearDownClass() {
        System.out.println("inside @AfterClass");
        System.out.println("=============================");
    }

    @Before //run before the test method
    public void setUp() {
        System.out.println();
       System.out.println("inside @Before");
    }

    @After //run after the test method
    public void tearDown() {
        System.out.println("inside @After");
        System.out.println();
    }

     @Test
     public void testMethod1() {
         System.out.println("inside @Test testMethod1");
     }

     @Test
     public void testMethod2() {
         System.out.println("inside @Test testMethod2");
     }
     @Test
     public void testMethod3() {
         System.out.println("inside @Test testMethod3");
     }
}

References:
http://www.mkyong.com/

Advertisements

About sivateja

I am a professional graduate..I am passionate about Latest Technology.
This entry was posted in Testing and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s