Showing posts with label Junit. Show all posts
Showing posts with label Junit. Show all posts

Junit Test with Maven

In this post I will explain how to create and run JUnit test using maven. download Apache maven from link “download”, extract zip package into you machine and setup path maven bin folder into your system  environment variable. following are the steps to create JUnit test for maven and maven commands.

JUnit Test creation:

Create source folder in your eclipse “src/test/java” and “src/main/java” as below screen:

Junit Test Suite

Test Suite means combined multiple test cases . when I run test suite all test case associated with suite will execute. In Junit we will create test suite with two ways Test Runner class or using annotation  @RnWith and @Suite, here is a example for both type. I have created two Junit test case “JunitTest1.java” and “JunitTest2.java”

package com.test;

import org.junit.*;

public class JunitTest1 {

    @BeforeClass
    public static void beforeClassAnnotation() {        
      System.out.println("@BeforeClass - for junit class 1");
    }

    @AfterClass
    public static void afterClassAnnotation() {       
      System.out.println("@AfterClass - for junit class 1");
    }  

    @Test
    public void testAnnotation() {       
        System.out.println("@Test - first class test method");
    }    
}


JUnit Test Template

In this topic I will explain the basic Junit framework template

package com.test;

import org.junit.*;

public class JunitTemplate {

    @BeforeClass
    public static void beforeClassAnnotation() {        
        System.out.println("@BeforeClass - execute one time before any method");
    }

    @AfterClass
    public static void afterClassAnnotation() {       
        System.out.println("@AfterClass - execute one time after last method");
    }

    @Before
    public void beforeAnnotation() {       
        System.out.println("@Before - execute before every method");
    }

    @After
    public void afterAnnotation() {      
        System.out.println("@After - execute after every method");
    }

    @Test
    public void testAnnotation() {       
        System.out.println("@Test - first test method");
    }

    @Test
    public void testAnnotation1() {       
        System.out.println("@Test - second test method");
    }
}

After execution above test execution log generated as below:

@BeforeClass - execute one time before any method
@Before - execute before every method
@Test - second test method
@After - execute after every method
@Before - execute before every method
@Test - first test method
@After - execute after every method
@AfterClass - execute one time after last method     

Junit testing framework

Junit is a unit test framework for implementing testing in java. However it is suitable for unit testing but for integration testing you should use TestNG framework instead of it.

Why use a testing framework?

Using a testing framework is beneficial because it forces you to explicitly declare the expected results of specific program execution routes. When debugging it is possible to write a test which expresses the result you are trying to achieve and then debug until the test comes out positive. By having a set of tests that test all the core components of the project it is possible to modify specific areas of the project and immediately see the effect the modifications have on the other areas by the results of the test, hence, side-effects can be quickly realized.
JUnit promotes the idea of first testing then coding, in that it is possible to setup test data for a unit which defines what the expected output is and then code until the tests pass. It is believed by some that this practice of "test a little, code a little, test a little, code a little..." increases programmer productivity and stability of program code whilst reducing programmer stress and the time spent debugging