Loading
 
What's Next: View All Tutorial →

Junit - A Simple program to test addition of two number and project setup

A test case defines the fixture to run multiple tests. To define a test case 1) implement a subclass of TestCase 2) define instance variables that store the state of the fixture 3) initialize the fixture state by overriding setUp 4) clean-up...

Published Date: 20/02/2015  Last Modified Date: 20/02/2015    New Post



A test case defines the fixture to run multiple tests. To define a test case
1) implement a subclass of TestCase
2) define instance variables that store the state of the fixture
3) initialize the fixture state by overriding setUp
4) clean-up after a test by overriding tearDown.
Each test runs in its own fixture so there can be no side effects among test runs

Maven Dependency

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


A Simple java program with two function to be tested

 

package com.mycompany.app;


public class Util {

    public static Integer add(Integer a, Integer b) {
        if (a == null) {
            return null;
        }
        if (b == null) {
            return null;
        }
        return a + b;
    }
    public static String getBaseUrl() {
        return "http://catchmycity.com";
    }
    public static String generateStoreDetails(String mobileNo) {

        String baseUrl = Util.getBaseUrl();

        return baseUrl + "/" + mobileNo;

    }

}


A Simple Junit test case to test the three methods

 
package com.mycompany.app;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class UtilTest {

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    public void testAdd() {
        System.out.println("add");
        Integer a = 15;
        Integer b = 15;
        Integer expResult = 30;
        Integer result = Util.add(a, b);
        assertEquals(expResult, result);

        assertEquals(null, Util.add(15, null));
        assertEquals(null, Util.add(null, 15));
        assertEquals(null, Util.add(null, null));
    }

    @Test
    public void testGetBaseUrl() {
        System.out.println("getBaseUrl");
        String expResult = "http://catchmycity.com";
        String result = Util.getBaseUrl();
        assertEquals(expResult, result);
    }

    @Test
    public void testGenerateStoreDetails() {
        System.out.println("generateStoreDetails");
        String mobileNo = "1234567890";
        String expResult = Util.getBaseUrl() + "/" + mobileNo;
        String result = Util.generateStoreDetails(mobileNo);
        assertEquals(expResult, result);
    }

}


Download Project

https://drive.google.com/file/d/0B_9jt9kIlxciaGdFcm9oUUQxS1E/view?usp=sharing
 
Awaiting for Administrator approval





Tags: JUnit

← Back to list


Related Post




×