aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/junitparams/usage/person_example/PersonTest.java
blob: 927204e9c8a5011f53962ae87fdd8a0fa9b1c8cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package junitparams.usage.person_example;

import static org.assertj.core.api.Assertions.assertThat;

import junitparams.naming.TestCaseName;
import org.junit.*;
import org.junit.runner.*;

import junitparams.*;

@RunWith(JUnitParamsRunner.class)
public class PersonTest {

    @Test
    @Parameters({
            "17, false",
            "22, true" })
    public void isAdultAgeDirect(int age, boolean valid) throws Exception {
        assertThat(new Person(age).isAdult()).isEqualTo(valid);
    }

    @Test
    @Parameters(method = "adultValues")
    public void isAdultAgeDefinedMethod(int age, boolean valid) throws Exception {
        assertThat(new Person(age).isAdult()).isEqualTo(valid);
    }

    private Object[] adultValues() {
        return new Object[]{new Object[]{17, false}, new Object[]{22, true}};
    }

    @Test
    @Parameters
    public void isAdultAgeDefaultMethod(int age, boolean valid) throws Exception {
        assertThat(new Person(age).isAdult()).isEqualTo(valid);
    }

    @SuppressWarnings("unused")
    private Object[] parametersForIsAdultAgeDefaultMethod() {
        return adultValues();
    }

    @Test
    @Parameters(source = PersonProvider.class)
    public void personIsAdult(Person person, boolean valid) {
        assertThat(person.isAdult()).isEqualTo(valid);
    }

    public static class PersonProvider {
        public static Object[] provideAdults() {
            return new Object[]{new Object[]{new Person(25), true}, new Object[]{new Person(32), true}};
        }

        public static Object[] provideTeens() {
            return new Object[]{new Object[]{new Person(12), false}, new Object[]{new Person(17), false}};
        }
    }

    @Test
    @Parameters(method = "adultValues")
    @TestCaseName("Is person with age {0} adult? It's {1} statement.")
    public void isAdultWithCustomTestName(int age, boolean valid) throws Exception {
        assertThat(new Person(age).isAdult()).isEqualTo(valid);
    }

    public static class Person {

        private String name;
        private int age;

        public Person(Integer age) {
            this.age = age;
        }

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public boolean isAdult() {
            return age >= 18;
        }

        public int getAge() {
            return age;
        }

        @Override
        public String toString() {
            return "Person of age: " + age;
        }
    }
}