• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package junitparams;
2 
3 import junitparams.internal.TestMethod;
4 import org.junit.Rule;
5 import org.junit.Test;
6 import org.junit.rules.ExpectedException;
7 import org.junit.runner.RunWith;
8 import org.junit.runners.model.FrameworkMethod;
9 import org.junit.runners.model.TestClass;
10 
11 import java.lang.reflect.Method;
12 
13 import static org.assertj.core.api.Assertions.assertThat;
14 
15 @RunWith(JUnitParamsRunner.class)
16 public class ParametersReaderProvidersTest {
17 
18     @Rule
19     public ExpectedException exception = ExpectedException.none();
20 
21     @Test
22     @Parameters(source = SingleParamSetProvider.class)
oneParamSetFromClass(String a, String b)23     public void oneParamSetFromClass(String a, String b) {
24         assertThat(a).isEqualTo("a");
25         assertThat(b).isEqualTo("b");
26     }
27 
28     public static class SingleParamSetProvider {
provideOneParamSetSameTypes()29         public static Object[] provideOneParamSetSameTypes() {
30             return new Object[]{"a", "b"};
31         }
32     }
33 
34     @Test
shouldPutProviderClassNameInExceptionMessageForProviderWithNoValidMethods()35     public void shouldPutProviderClassNameInExceptionMessageForProviderWithNoValidMethods() {
36         TestMethod testMethod = getTestMethodWithInvalidProvider();
37 
38         exception.expect(RuntimeException.class);
39         exception.expectMessage(ProviderClassWithNoValidMethods.class.getName());
40         testMethod.parametersSets();
41     }
42 
getTestMethodWithInvalidProvider()43     private TestMethod getTestMethodWithInvalidProvider() {
44         // Bug in original code relied on order of Class.getMethods() which is undefined.
45         try {
46             Method testMethod = TestClassWithProviderClassWithNoValidMethods.class
47                     .getDeclaredMethod("shouldDoNothingItsJustToConnectTestClassWithProvider");
48             return new TestMethod(new FrameworkMethod(testMethod), new TestClass(TestClassWithProviderClassWithNoValidMethods.class));
49         } catch (NoSuchMethodException e) {
50             throw new RuntimeException("Mismatch between method and test class", e);
51         }
52     }
53 
54     @RunWith(JUnitParamsRunner.class)
55     static class TestClassWithProviderClassWithNoValidMethods {
56         @Test
57         @Parameters(source = ProviderClassWithNoValidMethods.class)
shouldDoNothingItsJustToConnectTestClassWithProvider()58         public void shouldDoNothingItsJustToConnectTestClassWithProvider() {
59         }
60     }
61 
62     static class ProviderClassWithNoValidMethods {
63     }
64 
65     @Test
66     @Parameters(source = OneIntegerProvider.class)
providedPrimitiveParams(int integer)67     public void providedPrimitiveParams(int integer) {
68         assertThat(integer).isLessThan(4);
69     }
70 
71     public static class OneIntegerProvider {
provideTwoNumbers()72         public static Object[] provideTwoNumbers() {
73             return new Object[]{new Object[]{1}, new Object[]{2}};
74         }
75 
provideOneNumber()76         public static Object[] provideOneNumber() {
77             return new Object[]{new Object[]{3}};
78         }
79     }
80 
81     @Test
82     @Parameters(source = DomainObjectProvider.class)
providedDomainParams(DomainClass object1, DomainClass object2)83     public void providedDomainParams(DomainClass object1, DomainClass object2) {
84         assertThat(object1.toString()).isEqualTo("testNameOne");
85         assertThat(object2.toString()).isEqualTo("testNameTwo");
86     }
87 
88     public static class DomainObjectProvider {
provideDomainObject()89         public static Object[] provideDomainObject() {
90             return new Object[]{new Object[]{
91                     new DomainClass("testNameOne"),
92                     new DomainClass("testNameTwo")}};
93         }
94     }
95 
96     public static class DomainClass {
97         private final String name;
98 
DomainClass(String name)99         public DomainClass(String name) {
100             this.name = name;
101         }
102 
103         @Override
toString()104         public String toString() {
105             return name;
106         }
107     }
108 }
109