среда, 1 августа 2012 г.

testing: junit - parameterized tests


import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static java.lang.Math.abs;
import static java.lang.Math.sin;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(Parameterized.class)
public class ParameterizedTest {
    private double arg;
    private double expected;

    public ParameterizedTest(double arg, double expected) {
        this.arg = arg;
        this.expected = expected;
    }

    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {0, 0.0},
                {Math.PI / 2, 1.0},
                {Math.PI, 0.0},
                {3 * Math.PI / 2, -1.0},
        });
    }

    @Test
    public void test() {
        assertThat(abs(sin(arg) - expected) < 0.000001d, is(true));
    }
}