View Javadoc
1   package org.junit.runners.parameterized;
2   
3   import static java.util.Arrays.asList;
4   import static org.junit.Assert.assertEquals;
5   import static org.junit.Assert.assertNotEquals;
6   import static org.junit.rules.ExpectedException.none;
7   
8   import java.util.Arrays;
9   import java.util.List;
10  
11  import org.junit.Rule;
12  import org.junit.Test;
13  import org.junit.rules.ExpectedException;
14  import org.junit.runners.model.TestClass;
15  
16  public class TestWithParametersTest {
17      private static final String DUMMY_NAME = "dummy name";
18  
19      private static final TestClass DUMMY_TEST_CLASS = new TestClass(
20              DummyClass.class);
21  
22      private static final List<Object> DUMMY_PARAMETERS = Arrays
23              .<Object> asList("a", "b");
24  
25      @Rule
26      public final ExpectedException thrown = none();
27  
28      @Test
29      public void cannotBeCreatedWithoutAName() {
30          thrown.expect(NullPointerException.class);
31          thrown.expectMessage("The name is missing.");
32          new TestWithParameters(null, DUMMY_TEST_CLASS, DUMMY_PARAMETERS);
33      }
34  
35      @Test
36      public void cannotBeCreatedWithoutTestClass() {
37          thrown.expect(NullPointerException.class);
38          thrown.expectMessage("The test class is missing.");
39          new TestWithParameters(DUMMY_NAME, null, DUMMY_PARAMETERS);
40      }
41  
42      @Test
43      public void cannotBeCreatedWithoutParameters() {
44          thrown.expect(NullPointerException.class);
45          thrown.expectMessage("The parameters are missing.");
46          new TestWithParameters(DUMMY_NAME, DUMMY_TEST_CLASS,
47                  (List<Object>) null);
48      }
49  
50      @Test
51      public void doesNotAllowToModifyProvidedParameters() {
52          TestWithParameters test = new TestWithParameters(DUMMY_NAME,
53                  DUMMY_TEST_CLASS, DUMMY_PARAMETERS);
54          thrown.expect(UnsupportedOperationException.class);
55          test.getParameters().set(0, "another parameter");
56      }
57  
58      @Test
59      public void doesNotConsiderParametersWhichChangedAfterTestInstantiation() {
60          List<Object> parameters = Arrays.<Object> asList("dummy parameter");
61          TestWithParameters test = new TestWithParameters(DUMMY_NAME,
62                  DUMMY_TEST_CLASS, parameters);
63          parameters.set(0, "another parameter");
64          assertEquals(asList("dummy parameter"), test.getParameters());
65      }
66  
67      @Test
68      public void isEqualToTestWithSameNameAndTestClassAndParameters() {
69          TestWithParameters firstTest = new TestWithParameters(DUMMY_NAME,
70                  new TestClass(DummyClass.class), Arrays.<Object> asList("a",
71                          "b"));
72          TestWithParameters secondTest = new TestWithParameters(DUMMY_NAME,
73                  new TestClass(DummyClass.class), Arrays.<Object> asList("a",
74                          "b"));
75          assertEquals(firstTest, secondTest);
76      }
77  
78      @Test
79      public void isNotEqualToTestWithDifferentName() {
80          TestWithParameters firstTest = new TestWithParameters("name",
81                  DUMMY_TEST_CLASS, DUMMY_PARAMETERS);
82          TestWithParameters secondTest = new TestWithParameters("another name",
83                  DUMMY_TEST_CLASS, DUMMY_PARAMETERS);
84          assertNotEquals(firstTest, secondTest);
85      }
86  
87      @Test
88      public void isNotEqualToTestWithDifferentTestClass() {
89          TestWithParameters firstTest = new TestWithParameters(DUMMY_NAME,
90                  new TestClass(DummyClass.class), DUMMY_PARAMETERS);
91          TestWithParameters secondTest = new TestWithParameters(DUMMY_NAME,
92                  new TestClass(AnotherDummyClass.class), DUMMY_PARAMETERS);
93          assertNotEquals(firstTest, secondTest);
94      }
95  
96      @Test
97      public void isNotEqualToTestWithDifferentParameters() {
98          TestWithParameters firstTest = new TestWithParameters(DUMMY_NAME,
99                  DUMMY_TEST_CLASS, Arrays.<Object> asList("a"));
100         TestWithParameters secondTest = new TestWithParameters(DUMMY_NAME,
101                 DUMMY_TEST_CLASS, Arrays.<Object> asList("b"));
102         assertNotEquals(firstTest, secondTest);
103     }
104 
105     @Test
106     public void isNotEqualToObjectWithDifferentClass() {
107         TestWithParameters test = new TestWithParameters(DUMMY_NAME,
108                 DUMMY_TEST_CLASS, DUMMY_PARAMETERS);
109         assertNotEquals(test, new Integer(3));
110     }
111 
112     @Test
113     public void hasSameHashCodeAsEqualTest() {
114         TestWithParameters firstTest = new TestWithParameters(DUMMY_NAME,
115                 DUMMY_TEST_CLASS, DUMMY_PARAMETERS);
116         TestWithParameters secondTest = new TestWithParameters(DUMMY_NAME,
117                 DUMMY_TEST_CLASS, DUMMY_PARAMETERS);
118         assertEquals(firstTest.hashCode(), secondTest.hashCode());
119     }
120 
121     @Test
122     public void hasMeaningfulToString() {
123         TestWithParameters test = new TestWithParameters("name", new TestClass(
124                 DummyClass.class), Arrays.<Object> asList("first parameter",
125                 "second parameter"));
126         assertEquals(
127                 "Wrong toString().",
128                 "org.junit.runners.parameterized.TestWithParametersTest$DummyClass 'name' with parameters [first parameter, second parameter]",
129                 test.toString());
130     }
131 
132     private static class DummyClass {
133     }
134 
135     private static class AnotherDummyClass {
136     }
137 }