Skip to content

Instantly share code, notes, and snippets.

@DaddyMoe
Last active April 23, 2017 15:26
Show Gist options
  • Save DaddyMoe/a1b4539fa804e393ae158105a160b346 to your computer and use it in GitHub Desktop.
Save DaddyMoe/a1b4539fa804e393ae158105a160b346 to your computer and use it in GitHub Desktop.
Java Junit Parameterized tests for when you are not in the mood to repeat yourself DRY: source: https://github.com/junit-team/junit4/wiki/Parameterized-tests
import static org.junit.Assert.assertEquals;
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 JavaJunitParameterizedTest {
// variable labels from junit v4.12
@Parameters(name = "{index}: Fibonacci.compute({0}) expected to be: {1}")
public static Collection<Object[]> data() {
// The Test data
return Arrays.asList(new Object[][] {
{ 0, 0 }, // {INPUT, EXPECTED_RESULT}
{ 1, 1 },
{ 2, 1 },
{ 3, 2 }
});
}
private int fInput;
private int fExpected;
public JavaJunitParameterizedTest(int input, int expected) {
fInput= input;
fExpected= expected;
}
@Test
public void test() {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment