1 package org.junit.tests.running.methods;
2
3 import static org.hamcrest.CoreMatchers.allOf;
4 import static org.hamcrest.CoreMatchers.containsString;
5 import static org.junit.Assert.assertThat;
6
7 import java.util.Collection;
8 import java.util.HashSet;
9
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.junit.After;
13 import org.junit.AfterClass;
14 import org.junit.Before;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.junit.runner.Description;
18 import org.junit.runner.JUnitCore;
19 import org.junit.runner.Result;
20 import org.junit.runner.RunWith;
21 import org.junit.runners.JUnit4;
22
23 public class AnnotationTest extends TestCase {
24 static boolean run;
25
26 @Override
27 public void setUp() {
28 run = false;
29 }
30
31 static public class SimpleTest {
32 @Test
33 public void success() {
34 run = true;
35 }
36 }
37
38 public void testAnnotatedMethod() throws Exception {
39 JUnitCore runner = new JUnitCore();
40 runner.run(SimpleTest.class);
41 assertTrue(run);
42 }
43
44 @RunWith(JUnit4.class)
45 static public class SimpleTestWithFutureProofExplicitRunner {
46 @Test
47 public void success() {
48 run = true;
49 }
50 }
51
52 public void testAnnotatedMethodWithFutureProofExplicitRunner() throws Exception {
53 JUnitCore runner = new JUnitCore();
54 runner.run(SimpleTestWithFutureProofExplicitRunner.class);
55 assertTrue(run);
56 }
57
58 static public class SetupTest {
59 @Before
60 public void before() {
61 run = true;
62 }
63
64 @Test
65 public void success() {
66 }
67 }
68
69 public void testSetup() throws Exception {
70 JUnitCore runner = new JUnitCore();
71 runner.run(SetupTest.class);
72 assertTrue(run);
73 }
74
75 static public class TeardownTest {
76 @After
77 public void after() {
78 run = true;
79 }
80
81 @Test
82 public void success() {
83 }
84 }
85
86 public void testTeardown() throws Exception {
87 JUnitCore runner = new JUnitCore();
88 runner.run(TeardownTest.class);
89 assertTrue(run);
90 }
91
92 static public class FailureTest {
93 @Test
94 public void error() throws Exception {
95 org.junit.Assert.fail();
96 }
97 }
98
99 public void testRunFailure() throws Exception {
100 JUnitCore runner = new JUnitCore();
101 Result result = runner.run(FailureTest.class);
102 assertEquals(1, result.getRunCount());
103 assertEquals(1, result.getFailureCount());
104 assertEquals(AssertionError.class, result.getFailures().get(0).getException().getClass());
105 }
106
107 static public class SetupFailureTest {
108 @Before
109 public void before() {
110 throw new Error();
111 }
112
113 @Test
114 public void test() {
115 run = true;
116 }
117 }
118
119 public void testSetupFailure() throws Exception {
120 JUnitCore core = new JUnitCore();
121 Result runner = core.run(SetupFailureTest.class);
122 assertEquals(1, runner.getRunCount());
123 assertEquals(1, runner.getFailureCount());
124 assertEquals(Error.class, runner.getFailures().get(0).getException().getClass());
125 assertFalse(run);
126 }
127
128 static public class TeardownFailureTest {
129 @After
130 public void after() {
131 throw new Error();
132 }
133
134 @Test
135 public void test() {
136 }
137 }
138
139 public void testTeardownFailure() throws Exception {
140 JUnitCore core = new JUnitCore();
141 Result runner = core.run(TeardownFailureTest.class);
142 assertEquals(1, runner.getRunCount());
143 assertEquals(1, runner.getFailureCount());
144 assertEquals(Error.class, runner.getFailures().get(0).getException().getClass());
145 }
146
147 static public class TestAndTeardownFailureTest {
148 @After
149 public void after() {
150 throw new Error("hereAfter");
151 }
152
153 @Test
154 public void test() throws Exception {
155 throw new Exception("inTest");
156 }
157 }
158
159 public void testTestAndTeardownFailure() throws Exception {
160 JUnitCore core = new JUnitCore();
161 Result runner = core.run(TestAndTeardownFailureTest.class);
162 assertEquals(1, runner.getRunCount());
163 assertEquals(2, runner.getFailureCount());
164 assertThat(runner.getFailures().toString(), allOf(containsString("hereAfter"), containsString("inTest")));
165 }
166
167 static public class TeardownAfterFailureTest {
168 @After
169 public void after() {
170 run = true;
171 }
172
173 @Test
174 public void test() throws Exception {
175 throw new Exception();
176 }
177 }
178
179 public void testTeardownAfterFailure() throws Exception {
180 JUnitCore runner = new JUnitCore();
181 runner.run(TeardownAfterFailureTest.class);
182 assertTrue(run);
183 }
184
185 static int count;
186 static Collection<Object> tests;
187
188 static public class TwoTests {
189 @Test
190 public void one() {
191 count++;
192 tests.add(this);
193 }
194
195 @Test
196 public void two() {
197 count++;
198 tests.add(this);
199 }
200 }
201
202 public void testTwoTests() throws Exception {
203 count = 0;
204 tests = new HashSet<Object>();
205 JUnitCore runner = new JUnitCore();
206 runner.run(TwoTests.class);
207 assertEquals(2, count);
208 assertEquals(2, tests.size());
209 }
210
211 static public class OldTest extends TestCase {
212 public void test() {
213 run = true;
214 }
215 }
216
217 public void testOldTest() throws Exception {
218 JUnitCore runner = new JUnitCore();
219 runner.run(OldTest.class);
220 assertTrue(run);
221 }
222
223 static public class OldSuiteTest extends TestCase {
224 public void testOne() {
225 run = true;
226 }
227 }
228
229 public void testOldSuiteTest() throws Exception {
230 TestSuite suite = new TestSuite(OldSuiteTest.class);
231 JUnitCore runner = new JUnitCore();
232 runner.run(suite);
233 assertTrue(run);
234 }
235
236 static public class ExceptionTest {
237 @Test(expected = Error.class)
238 public void expectedException() {
239 throw new Error();
240 }
241 }
242
243 public void testException() throws Exception {
244 JUnitCore core = new JUnitCore();
245 Result result = core.run(ExceptionTest.class);
246 assertEquals(0, result.getFailureCount());
247 }
248
249 static public class NoExceptionTest {
250 @Test(expected = Error.class)
251 public void expectedException() {
252 }
253 }
254
255 public void testExceptionNotThrown() throws Exception {
256 JUnitCore core = new JUnitCore();
257 Result result = core.run(NoExceptionTest.class);
258 assertEquals(1, result.getFailureCount());
259 assertEquals("Expected exception: java.lang.Error", result.getFailures().get(0).getMessage());
260 }
261
262 static public class OneTimeSetup {
263 @BeforeClass
264 public static void once() {
265 count++;
266 }
267
268 @Test
269 public void one() {
270 }
271
272 @Test
273 public void two() {
274 }
275 }
276
277 public void testOneTimeSetup() throws Exception {
278 count = 0;
279 JUnitCore core = new JUnitCore();
280 core.run(OneTimeSetup.class);
281 assertEquals(1, count);
282 }
283
284 static public class OneTimeTeardown {
285 @AfterClass
286 public static void once() {
287 count++;
288 }
289
290 @Test
291 public void one() {
292 }
293
294 @Test
295 public void two() {
296 }
297 }
298
299 public void testOneTimeTeardown() throws Exception {
300 count = 0;
301 JUnitCore core = new JUnitCore();
302 core.run(OneTimeTeardown.class);
303 assertEquals(1, count);
304 }
305
306 static String log;
307
308 public static class OrderTest {
309 @BeforeClass
310 public static void onceBefore() {
311 log += "beforeClass ";
312 }
313
314 @Before
315 public void before() {
316 log += "before ";
317 }
318
319 @Test
320 public void test() {
321 log += "test ";
322 }
323
324 @After
325 public void after() {
326 log += "after ";
327 }
328
329 @AfterClass
330 public static void onceAfter() {
331 log += "afterClass ";
332 }
333 }
334
335 public void testOrder() throws Exception {
336 log = "";
337 JUnitCore core = new JUnitCore();
338 core.run(OrderTest.class);
339 assertEquals("beforeClass before test after afterClass ", log);
340 }
341
342 static public class NonStaticOneTimeSetup {
343 @BeforeClass
344 public void once() {
345 }
346
347 @Test
348 public void aTest() {
349 }
350 }
351
352 public void testNonStaticOneTimeSetup() throws Exception {
353 JUnitCore core = new JUnitCore();
354 Result result = core.run(NonStaticOneTimeSetup.class);
355 assertEquals(1, result.getFailureCount());
356 }
357
358 static public class ErrorInBeforeClass {
359 @BeforeClass
360 public static void before() throws Exception {
361 throw new Exception();
362 }
363
364 @Test
365 public void test() {
366 run = true;
367 }
368 }
369
370 public void testErrorInBeforeClass() throws Exception {
371 run = false;
372 JUnitCore core = new JUnitCore();
373 Result result = core.run(ErrorInBeforeClass.class);
374 assertFalse(run);
375 assertEquals(1, result.getFailureCount());
376 Description description = result.getFailures().get(0).getDescription();
377 assertEquals(ErrorInBeforeClass.class.getName(), description.getDisplayName());
378 }
379
380 static public class ErrorInAfterClass {
381 @Test
382 public void test() {
383 run = true;
384 }
385
386 @AfterClass
387 public static void after() throws Exception {
388 throw new Exception();
389 }
390 }
391
392 public void testErrorInAfterClass() throws Exception {
393 run = false;
394 JUnitCore core = new JUnitCore();
395 Result result = core.run(ErrorInAfterClass.class);
396 assertTrue(run);
397 assertEquals(1, result.getFailureCount());
398 }
399
400 static public class SuperInheritance {
401 @BeforeClass
402 static public void beforeClassSuper() {
403 log += "Before class super ";
404 }
405
406 @AfterClass
407 static public void afterClassSuper() {
408 log += "After class super ";
409 }
410
411 @Before
412 public void beforeSuper() {
413 log += "Before super ";
414 }
415
416 @After
417 public void afterSuper() {
418 log += "After super ";
419 }
420 }
421
422 static public class SubInheritance extends SuperInheritance {
423 @BeforeClass
424 static public void beforeClassSub() {
425 log += "Before class sub ";
426 }
427
428 @AfterClass
429 static public void afterClassSub() {
430 log += "After class sub ";
431 }
432
433 @Before
434 public void beforeSub() {
435 log += "Before sub ";
436 }
437
438 @After
439 public void afterSub() {
440 log += "After sub ";
441 }
442
443 @Test
444 public void test() {
445 log += "Test ";
446 }
447 }
448
449 public void testOrderingOfInheritance() throws Exception {
450 log = "";
451 JUnitCore core = new JUnitCore();
452 core.run(SubInheritance.class);
453 assertEquals("Before class super Before class sub Before super Before sub Test After sub After super After class sub After class super ", log);
454 }
455
456 static public class SuperShadowing {
457 @Before
458 public void before() {
459 log += "Before super ";
460 }
461
462 @After
463 public void after() {
464 log += "After super ";
465 }
466 }
467
468 static public class SubShadowing extends SuperShadowing {
469 @Override
470 @Before
471 public void before() {
472 log += "Before sub ";
473 }
474
475 @Override
476 @After
477 public void after() {
478 log += "After sub ";
479 }
480
481 @Test
482 public void test() {
483 log += "Test ";
484 }
485 }
486
487 public void testShadowing() throws Exception {
488 log = "";
489 JUnitCore core = new JUnitCore();
490 core.run(SubShadowing.class);
491 assertEquals("Before sub Test After sub ", log);
492 }
493
494 static public class SuperTest {
495 @Test
496 public void one() {
497 log += "Super";
498 }
499
500 @Test
501 public void two() {
502 log += "Two";
503 }
504 }
505
506 static public class SubTest extends SuperTest {
507 @Override
508 @Test
509 public void one() {
510 log += "Sub";
511 }
512 }
513
514 public void testTestInheritance() throws Exception {
515 log = "";
516 JUnitCore core = new JUnitCore();
517 core.run(SubTest.class);
518
519 assertTrue(log.contains("Sub"));
520 assertTrue(log.contains("Two"));
521 assertFalse(log.contains("Super"));
522 }
523
524 static public class RunAllAfters {
525 @Before
526 public void good() {
527 }
528
529 @Before
530 public void bad() {
531 throw new Error();
532 }
533
534 @Test
535 public void empty() {
536 }
537
538 @After
539 public void one() {
540 log += "one";
541 }
542
543 @After
544 public void two() {
545 log += "two";
546 }
547 }
548
549 public void testRunAllAfters() {
550 log = "";
551 JUnitCore core = new JUnitCore();
552 core.run(RunAllAfters.class);
553 assertTrue(log.contains("one"));
554 assertTrue(log.contains("two"));
555 }
556
557 static public class RunAllAftersRegardless {
558 @Test
559 public void empty() {
560 }
561
562 @After
563 public void one() {
564 log += "one";
565 throw new Error();
566 }
567
568 @After
569 public void two() {
570 log += "two";
571 throw new Error();
572 }
573 }
574
575 public void testRunAllAftersRegardless() {
576 log = "";
577 JUnitCore core = new JUnitCore();
578 Result result = core.run(RunAllAftersRegardless.class);
579 assertTrue(log.contains("one"));
580 assertTrue(log.contains("two"));
581 assertEquals(2, result.getFailureCount());
582 }
583
584 static public class RunAllAfterClasses {
585 @Before
586 public void good() {
587 }
588
589 @BeforeClass
590 public static void bad() {
591 throw new Error();
592 }
593
594 @Test
595 public void empty() {
596 }
597
598 @AfterClass
599 public static void one() {
600 log += "one";
601 }
602
603 @AfterClass
604 public static void two() {
605 log += "two";
606 }
607 }
608
609 public void testRunAllAfterClasses() {
610 log = "";
611 JUnitCore core = new JUnitCore();
612 core.run(RunAllAfterClasses.class);
613 assertTrue(log.contains("one"));
614 assertTrue(log.contains("two"));
615 }
616
617 static public class RunAllAfterClassesRegardless {
618 @Test
619 public void empty() {
620 }
621
622 @AfterClass
623 static public void one() {
624 log += "one";
625 throw new Error();
626 }
627
628 @AfterClass
629 static public void two() {
630 log += "two";
631 throw new Error();
632 }
633 }
634
635 public void testRunAllAfterClassesRegardless() {
636 log = "";
637 JUnitCore core = new JUnitCore();
638 Result result = core.run(RunAllAfterClassesRegardless.class);
639 assertTrue(log.contains("one"));
640 assertTrue(log.contains("two"));
641 assertEquals(2, result.getFailureCount());
642 }
643 }