1 package org.junit.tests.running.methods;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5
6 import java.util.Collections;
7 import java.util.List;
8
9 import junit.framework.JUnit4TestAdapter;
10 import junit.framework.TestResult;
11 import org.junit.After;
12 import org.junit.AfterClass;
13 import org.junit.Before;
14 import org.junit.BeforeClass;
15 import org.junit.Ignore;
16 import org.junit.Test;
17 import org.junit.runner.JUnitCore;
18 import org.junit.runner.Result;
19 import org.junit.runners.BlockJUnit4ClassRunner;
20 import org.junit.runners.model.InitializationError;
21
22 public class TestMethodTest {
23
24 @SuppressWarnings("all")
25 public static class EverythingWrong {
26 private EverythingWrong() {
27 }
28
29 @BeforeClass
30 public void notStaticBC() {
31 }
32
33 @BeforeClass
34 static void notPublicBC() {
35 }
36
37 @BeforeClass
38 public static int nonVoidBC() {
39 return 0;
40 }
41
42 @BeforeClass
43 public static void argumentsBC(int i) {
44 }
45
46 @BeforeClass
47 public static void fineBC() {
48 }
49
50 @AfterClass
51 public void notStaticAC() {
52 }
53
54 @AfterClass
55 static void notPublicAC() {
56 }
57
58 @AfterClass
59 public static int nonVoidAC() {
60 return 0;
61 }
62
63 @AfterClass
64 public static void argumentsAC(int i) {
65 }
66
67 @AfterClass
68 public static void fineAC() {
69 }
70
71 @After
72 public static void staticA() {
73 }
74
75 @After
76 void notPublicA() {
77 }
78
79 @After
80 public int nonVoidA() {
81 return 0;
82 }
83
84 @After
85 public void argumentsA(int i) {
86 }
87
88 @After
89 public void fineA() {
90 }
91
92 @Before
93 public static void staticB() {
94 }
95
96 @Before
97 void notPublicB() {
98 }
99
100 @Before
101 public int nonVoidB() {
102 return 0;
103 }
104
105 @Before
106 public void argumentsB(int i) {
107 }
108
109 @Before
110 public void fineB() {
111 }
112
113 @Test
114 public static void staticT() {
115 }
116
117 @Test
118 void notPublicT() {
119 }
120
121 @Test
122 public int nonVoidT() {
123 return 0;
124 }
125
126 @Test
127 public void argumentsT(int i) {
128 }
129
130 @Test
131 public void fineT() {
132 }
133 }
134
135 @Test
136 public void testFailures() throws Exception {
137 List<Throwable> problems = validateAllMethods(EverythingWrong.class);
138 int errorCount = 1 + 4 * 5;
139 assertEquals(errorCount, problems.size());
140 }
141
142 static public class SuperWrong {
143 @Test
144 void notPublic() {
145 }
146 }
147
148 static public class SubWrong extends SuperWrong {
149 @Test
150 public void justFine() {
151 }
152 }
153
154 @Test
155 public void validateInheritedMethods() throws Exception {
156 List<Throwable> problems = validateAllMethods(SubWrong.class);
157 assertEquals(1, problems.size());
158 }
159
160 static public class SubShadows extends SuperWrong {
161 @Override
162 @Test
163 public void notPublic() {
164 }
165 }
166
167 @Test
168 public void dontValidateShadowedMethods() throws Exception {
169 List<Throwable> problems = validateAllMethods(SubShadows.class);
170 assertTrue(problems.isEmpty());
171 }
172
173 private List<Throwable> validateAllMethods(Class<?> clazz) {
174 try {
175 new BlockJUnit4ClassRunner(clazz);
176 } catch (InitializationError e) {
177 return e.getCauses();
178 }
179 return Collections.emptyList();
180 }
181
182 static public class IgnoredTest {
183 @Test
184 public void valid() {
185 }
186
187 @Ignore
188 @Test
189 public void ignored() {
190 }
191
192 @Ignore("For testing purposes")
193 @Test
194 public void withReason() {
195 }
196 }
197
198 @Test
199 public void ignoreRunner() {
200 JUnitCore runner = new JUnitCore();
201 Result result = runner.run(IgnoredTest.class);
202 assertEquals(2, result.getIgnoreCount());
203 assertEquals(1, result.getRunCount());
204 }
205
206 @Test
207 public void compatibility() {
208 TestResult result = new TestResult();
209 new JUnit4TestAdapter(IgnoredTest.class).run(result);
210 assertEquals(1, result.runCount());
211 }
212
213 public static class Confused {
214 @Test
215 public void a(Object b) {
216 }
217
218 @Test
219 public void a() {
220 }
221 }
222
223 @Test(expected = InitializationError.class)
224 public void overloaded() throws InitializationError {
225 new BlockJUnit4ClassRunner(Confused.class);
226 }
227
228 public static class ConstructorParameter {
229 public ConstructorParameter(Object something) {
230 }
231
232 @Test
233 public void a() {
234 }
235 }
236
237 @Test(expected = InitializationError.class)
238 public void constructorParameter() throws InitializationError {
239 new BlockJUnit4ClassRunner(ConstructorParameter.class);
240 }
241
242 public static class OnlyTestIsIgnored {
243 @Ignore
244 @Test
245 public void ignored() {
246 }
247 }
248
249 @Test
250 public void onlyIgnoredMethodsIsStillFineTestClass() {
251 Result result = JUnitCore.runClasses(OnlyTestIsIgnored.class);
252 assertEquals(0, result.getFailureCount());
253 assertEquals(1, result.getIgnoreCount());
254 }
255 }