Friday, August 17, 2012

JUnit 4 Tutorial , Parameterized Test

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class SimpleMathTest {

    public static class SimpeMath {
        public static int Add(int a, int b) {
            return a + b;
        }
    }

    int a, b, expected;

    public SimpleMathTest(int a, int b, int expected) {
        super();
        this.a = a;
        this.b = b;
        this.expected = expected;
    }

    @Parameters
    public static Collection < Object[] > TestThoseSeries() {

        Object[][] data = new Object[][] {
            {
                1, 2, 3
            }, {
                2, 2, 4
            }, {
                3, 3, 6
            }, {
                4, 4, 9
            }
        };
        return Arrays.asList(data);
    }

    @Test
    public void testAdd() {
        assertSame(expected, SimpeMath.Add(a, b));
    }

}

you will see 3 passed, one failed when you run the test.

image

No comments:

 
Locations of visitors to this page