Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save childnode/c954c81360f1052722aa20ba1291485f to your computer and use it in GitHub Desktop.
Save childnode/c954c81360f1052722aa20ba1291485f to your computer and use it in GitHub Desktop.
JUnit Parameterized Tests with Annotations https://github.com/junit-team/junit4/wiki/Parameterized-tests
package de.childno.test.java.junit;
import com.google.auto.value.AutoValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNot.not;
import static java.util.Collections.singletonList;
@RunWith(Parameterized.class)
public class ComplexDataProviderWithAutoValueTest
{
@SuppressWarnings("WeakerAccess")
@Parameterized.Parameter(0)
public static MyTestPermutation runWithParameters;
@SuppressWarnings("WeakerAccess")
@Parameterized.Parameter(1)
public static MyValidationState runWithExpectation;
@Parameterized.Parameters(name = "{0} becomes {1}")
public static Collection<Object[]> permutationDataProvider() {
ArrayList<Object[]> testData = new ArrayList<>();
for (MyEnumCategory cat : runWithCategories()) {
for (int cardinal : runWithCardinals()) {
list.add(
new Object[] {
MyTestPermutation.create(cat, cardinal),
(cardinal > 0 && cat != MyEnumCategory.INVALID) ? MyValidationState.ACCEPT : MyValidationState.DECLINE
}
);
}
}
return testData;
}
@Test
public void TestCaseWithPermutation()
{
MyValidator validator = MyMessageFactory.get();
validator.check(runWithParameters.enumeration(), runWithParameters.cardinal());
assertThat(validator.result(), is(runWithExpectation));
}
@AutoValue
abstract class MyTestPermutation {
static MyTestPermutation create(MyEnum enumeration, int cardinal) {
return new AutoValue_MyTestPermutation(enumeration, cardinal);
}
abstract MyEnum enumeration();
abstract int cardinal();
}
}
package de.childno.test.java.junit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNot.not;
import static java.util.Collections.singletonList;
@RunWith(Parameterized.class)
public class EnhancedDataProviderTest
{
@SuppressWarnings("WeakerAccess")
@Parameterized.Parameter(0)
public static MyEnum runWithEnum;
@SuppressWarnings("WeakerAccess")
@Parameterized.Parameter(1)
public static int runWithInt;
@SuppressWarnings("WeakerAccess")
@Parameterized.Parameter(2)
public static List<String> runWithList;
@Parameterized.Parameters(name = "{0}")
public static Object[][] enhancedDataProvider()
{
return new Object[][] {
{
"testname",
MyEnum.FIRST,
1,
Arrays.asList("0", "1")
},
{
"testname",
MyEnum.SECOND,
2,
Arrays.asList("0", "1", "2")
}
}
}
@Test
public void TestCaseWithEnumIntAndList()
{
actual = MyClassFactory.get(runWithEnum);
assertThat(actual.state, is("set" + runWithEnum.toString()));
assertThat(actual.cardinal(), is(runWithInt));
assertThat(actual.list, is(expected.list));
}
}
package de.childno.test.java.junit;
import com.google.auto.value.AutoValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNot.not;
import static java.util.Collections.singletonList;
@RunWith(Parameterized.class)
public class EnhancedDataProviderWithAutoValueTest
{
/**
* this field is necessary since Junit throws an exception if a parameter field is not seen
*/
@SuppressWarnings("WeakerAccess")
@SuppressWarnings("unused")
@Parameterized.Parameter
public static String _ignore_TestCaseName;
@SuppressWarnings("WeakerAccess")
@Parameterized.Parameter(1)
public static MyTestPermutation runWithParameters;
@SuppressWarnings("WeakerAccess")
@Parameterized.Parameter(2)
public static String runWithExpectation;
@Parameterized.Parameters(name = "{0}")
public static Object[][] enhancedDataProviderAutoValue()
{
return new Object[][] {
{
"positiveTest",
MyTestPermutation.create(MyEnumCategory.FIRST, 1),
"1st expectation for #1"
},
{
"negetiveTest",
MyTestPermutation.create(MyEnumCategory.SECOND, 2),
"2nd expectation for #2"
}
}
}
@Test
public void TestCaseWithEnum()
{
MyMessage actual = MyMessageFactory.get(runWithParameters.enumeration());
assertThat(actual.message(), is(runWithExpectation));
}
@Test
public void TestCaseWithInt()
{
MyMessage actual = MyMessageFactory.get(runWithParameters.cardinal());
assertThat(actual.message(), is(runWithExpectation));
}
@AutoValue
abstract class MyTestPermutation {
static MyTestPermutation create(MyEnum enumeration, int cardinal) {
return new AutoValue_MyTestPermutation(enumeration, cardinal);
}
abstract MyEnum enumeration();
abstract int cardinal();
}
}
package de.childno.test.java.junit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNot.not;
@RunWith(Parameterized.class)
public class SimpleDataProviderTest
{
@SuppressWarnings("WeakerAccess")
@Parameterized.Parameter
public static MyEnum runWithEnum;
@Parameterized.Parameters(name = "{0}")
public static MyEnum[] myEnumDataProvider()
{
return MyEnum.values();
}
@Test
public void TestCaseWithEnum()
{
expected = new MyClass();
expected.state = "set" + runWithEnum.toString();
actual = MyClassFactory.get(runWithEnum);
assertThat(actual, is(expected));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment