1 package junitparams; 2 3 import static org.assertj.core.api.Assertions.assertThat; 4 5 import java.util.Arrays; 6 import java.util.List; 7 8 import junitparams.usage.person_example.PersonTest; 9 import junitparams.usage.person_example.PersonTest.Person; 10 11 import org.junit.Test; 12 import org.junit.runner.RunWith; 13 14 @SuppressWarnings("unused") 15 @RunWith(JUnitParamsRunner.class) 16 public class MethodAnnotationArgumentTest { 17 18 @Test 19 @Parameters(method = "return1") testSingleMethodName(int number)20 public void testSingleMethodName(int number) { 21 assertThat(number).isEqualTo(1); 22 } 23 return1()24 private Integer[] return1() { 25 return new Integer[] { 1 }; 26 } 27 28 @Test 29 @Parameters(method = "return1,return2") testMultipleMethodNames(int number)30 public void testMultipleMethodNames(int number) { 31 assertThat(number) 32 .isLessThanOrEqualTo(2) 33 .isGreaterThanOrEqualTo(1); 34 } 35 36 @Test 37 @Parameters(method = "return1, return2") testMultipleMethodNamesWithWhitespaces(int number)38 public void testMultipleMethodNamesWithWhitespaces(int number) { 39 assertThat(number) 40 .isLessThanOrEqualTo(2) 41 .isGreaterThanOrEqualTo(1); 42 } 43 return2()44 private Integer[] return2() { 45 return new Integer[] { 2 }; 46 } 47 48 @Test 49 @Parameters(source = PersonTest.class, method = "adultValues") testSingleMethodFromDifferentClass(int age, boolean valid)50 public void testSingleMethodFromDifferentClass(int age, boolean valid) { 51 assertThat(new Person(age).isAdult()).isEqualTo(valid); 52 } 53 54 @Test 55 @Parameters(method = "stringParamsWithNull") shouldPassStringParamsWithNullFromMethod(String parameter)56 public void shouldPassStringParamsWithNullFromMethod(String parameter) { 57 // given 58 List<String> acceptedParams = Arrays.asList("1", "2", "3", null); 59 60 // then 61 assertThat(acceptedParams).contains(parameter); 62 } 63 stringParamsWithNull()64 Object[] stringParamsWithNull() { 65 return genericArray("1", "2", "3", null); 66 } 67 68 @Test 69 @Parameters(method = "multiStringParams") shouldPassMultiStringParams(String first, String second)70 public void shouldPassMultiStringParams(String first, String second) { 71 assertThat(first).isEqualTo(second); 72 } 73 multiStringParams()74 Object[] multiStringParams() { 75 return genericArray( 76 genericArray("test", "test"), 77 genericArray("ble", "ble")); 78 } 79 genericArray(T... elements)80 private static <T> T[] genericArray(T... elements) { 81 return elements; 82 } 83 } 84