Home | History | Annotate | Download | only in person_example
      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" })
     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")
     24     public void isAdultAgeDefinedMethod(int age, boolean valid) throws Exception {
     25         assertThat(new Person(age).isAdult()).isEqualTo(valid);
     26     }
     27 
     28     private Object[] adultValues() {
     29         return new Object[]{new Object[]{17, false}, new Object[]{22, true}};
     30     }
     31 
     32     @Test
     33     @Parameters
     34     public void isAdultAgeDefaultMethod(int age, boolean valid) throws Exception {
     35         assertThat(new Person(age).isAdult()).isEqualTo(valid);
     36     }
     37 
     38     @SuppressWarnings("unused")
     39     private Object[] parametersForIsAdultAgeDefaultMethod() {
     40         return adultValues();
     41     }
     42 
     43     @Test
     44     @Parameters(source = PersonProvider.class)
     45     public void personIsAdult(Person person, boolean valid) {
     46         assertThat(person.isAdult()).isEqualTo(valid);
     47     }
     48 
     49     public static class PersonProvider {
     50         public static Object[] provideAdults() {
     51             return new Object[]{new Object[]{new Person(25), true}, new Object[]{new Person(32), true}};
     52         }
     53 
     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.")
     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 
     74         public Person(Integer age) {
     75             this.age = age;
     76         }
     77 
     78         public Person(String name, Integer age) {
     79             this.name = name;
     80             this.age = age;
     81         }
     82 
     83         public String getName() {
     84             return name;
     85         }
     86 
     87         public boolean isAdult() {
     88             return age >= 18;
     89         }
     90 
     91         public int getAge() {
     92             return age;
     93         }
     94 
     95         @Override
     96         public String toString() {
     97             return "Person of age: " + age;
     98         }
     99     }
    100 }