Wednesday, June 8, 2011

How to Write java Unit Test class and Run

normally.................you can download junit.jar file and add to the project.

But in maven project, You should add the following dependency for download junit.jar file

    <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>


This is small Testing Example...................................




import java.util.ArrayList;
import java.util.List;

public class StringConverter {


    /*if input is String List, you can get Comma seperated String by using this method*/

    public String convertToString(List<String> stringList) {

        StringBuilder concatanatedString = new StringBuilder();
        for(String string : stringList) {
            concatanatedString.append(string);
            concatanatedString.append(",");
        }
        String commaSeperatedString;
        try{
        commaSeperatedString = concatanatedString.toString().substring(0,
                concatanatedString.toString().length() - 1);
        }catch (Exception e){
            commaSeperatedString ="";
        }
        return commaSeperatedString;


    }


    /*if input is comma seperated string, you can get String List by using this method*/

    public List<String> convertToSet(String commaSeperatedList) {
       String[] StringArray = commaSeperatedList.split(",");
       List<String> stringList = new ArrayList<String>();
       for (int i = 0; i < StringArray.length ;i++){
          stringList.add(StringArray[i].trim());
       }
       return stringList;

    }

}


This unit Testing class for StringConverter......................
@BeforeClass ------------------> Setup all variable
@Test---------------------------> run test method.............


import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.Assert;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class StringConverterTest {

    private StringConverter converter;

    private List<String> List1;
    private String stringComma;

    @BeforeClass
    public void setup() {
        converter = new StringConverter();
        List1 = new ArrayList<String>();
        List1.add("st1");
        List1.add("st2");
        List1.add("st3");
        List1.add("st4");
        List1.add("st5");
        List1.add("st6");
        stringComma = "aa,bb,pp,dd,ee,ff,mm";
    }

    @AfterClass
    public void tearDown() {

    }

    @Test
    public void testConvertToStringSuccess() {
        String out = converter.convertToString(List1);
        Assert.assertEquals("st1,st2,st3,st4,st5,st6", out);
    }

    @Test
    public void testConvertToListSuccess() {
        List<String> stringList = converter.convertToSet(stringComma);
        Assert.assertEquals("aa", stringList.get(0));
        Assert.assertEquals("pp", stringList.get(2));
        Assert.assertEquals("ee", stringList.get(4));
        Assert.assertEquals("mm", stringList.get(6));
    }

}


If you want to run single test you can use following command
mvn clean -Dtestcase=<testcase-class-name> test:single

If you want to run all test case, you can use
mvn clean install





Dileepa Sisila Chandrasekera
Computer Science & Engineering
University of Moratuwa
Trainee at hsenid mobile solution (pvt) Ltd.













No comments:

Post a Comment