1 package junitparams.usage.person_example;
2 
3 import static org.assertj.core.api.Assertions.assertThat;
4 
5 import junitparams.naming.TestCaseName;
6 import org.junit.*;
7 import org.junit.runner.*;
8 
9 import junitparams.*;
10 
11 @RunWith(JUnitParamsRunner.class)
12 public class PersonTest {
13 
14     @Test
15     @Parameters({
16             "17, false",
17             "22, true" })
isAdultAgeDirect(int age, boolean valid)18     public void isAdultAgeDirect(int age, boolean valid) throws Exception {
19         assertThat(new Person(age).isAdult()).isEqualTo(valid);
20     }
21 
22     @Test
23     @Parameters(method = "adultValues")
isAdultAgeDefinedMethod(int age, boolean valid)24     public void isAdultAgeDefinedMethod(int age, boolean valid) throws Exception {
25         assertThat(new Person(age).isAdult()).isEqualTo(valid);
26     }
27 
adultValues()28     private Object[] adultValues() {
29         return new Object[]{new Object[]{17, false}, new Object[]{22, true}};
30     }
31 
32     @Test
33     @Parameters
isAdultAgeDefaultMethod(int age, boolean valid)34     public void isAdultAgeDefaultMethod(int age, boolean valid) throws Exception {
35         assertThat(new Person(age).isAdult()).isEqualTo(valid);
36     }
37 
38     @SuppressWarnings("unused")
parametersForIsAdultAgeDefaultMethod()39     private Object[] parametersForIsAdultAgeDefaultMethod() {
40         return adultValues();
41     }
42 
43     @Test
44     @Parameters(source = PersonProvider.class)
personIsAdult(Person person, boolean valid)45     public void personIsAdult(Person person, boolean valid) {
46         assertThat(person.isAdult()).isEqualTo(valid);
47     }
48 
49     public static class PersonProvider {
provideAdults()50         public static Object[] provideAdults() {
51             return new Object[]{new Object[]{new Person(25), true}, new Object[]{new Person(32), true}};
52         }
53 
provideTeens()54         public static Object[] provideTeens() {
55             return new Object[]{new Object[]{new Person(12), false}, new Object[]{new Person(17), false}};
56         }
57     }
58 
59     // Android-changed: CTS and AndroidJUnitRunner rely on specific format to test names, changing
60     // them will prevent CTS and AndroidJUnitRunner from working properly; see b/36541809
61     @Ignore
62     @Test
63     @Parameters(method = "adultValues")
64     @TestCaseName("Is person with age {0} adult? It's {1} statement.")
isAdultWithCustomTestName(int age, boolean valid)65     public void isAdultWithCustomTestName(int age, boolean valid) throws Exception {
66         assertThat(new Person(age).isAdult()).isEqualTo(valid);
67     }
68 
69     public static class Person {
70 
71         private String name;
72         private int age;
73 
Person(Integer age)74         public Person(Integer age) {
75             this.age = age;
76         }
77 
Person(String name, Integer age)78         public Person(String name, Integer age) {
79             this.name = name;
80             this.age = age;
81         }
82 
getName()83         public String getName() {
84             return name;
85         }
86 
isAdult()87         public boolean isAdult() {
88             return age >= 18;
89         }
90 
getAge()91         public int getAge() {
92             return age;
93         }
94 
95         @Override
toString()96         public String toString() {
97             return "Person of age: " + age;
98         }
99     }
100 }