1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.common.junit4;
20
21 import java.util.concurrent.CountDownLatch;
22
23 import junit.framework.Assert;
24 import junit.framework.TestCase;
25 import org.apache.maven.surefire.api.util.internal.DaemonThreadFactory;
26 import org.junit.Test;
27 import org.junit.runner.Description;
28 import org.junit.runner.Request;
29 import org.junit.runner.Runner;
30 import org.junit.runner.notification.Failure;
31 import org.junit.runner.notification.RunListener;
32 import org.junit.runner.notification.RunNotifier;
33
34
35
36
37 public class JUnit4RunListenerTest extends TestCase {
38 public void testTestStarted() {
39 RunListener jUnit4TestSetReporter = new JUnit4RunListener(new MockReporter());
40 Runner junitTestRunner =
41 Request.classes("abc", STest1.class, STest2.class).getRunner();
42 RunNotifier runNotifier = new RunNotifier();
43 runNotifier.addListener(jUnit4TestSetReporter);
44 junitTestRunner.run(runNotifier);
45 }
46
47 public void testParallelInvocations() throws Exception {
48 final MockReporter reporter = new MockReporter();
49 final RunListener jUnit4TestSetReporter = new JUnit4RunListener(reporter);
50 final CountDownLatch countDownLatch = new CountDownLatch(1);
51 final Description testSomething = Description.createTestDescription(STest1.class, "testSomething");
52 final Description testSomething2 = Description.createTestDescription(STest2.class, "testSomething2");
53
54 jUnit4TestSetReporter.testStarted(testSomething);
55
56 DaemonThreadFactory.newDaemonThread(new Runnable() {
57 @Override
58 public void run() {
59 try {
60 jUnit4TestSetReporter.testStarted(testSomething2);
61 jUnit4TestSetReporter.testFailure(new Failure(testSomething2, new AssertionError("Fud")));
62 jUnit4TestSetReporter.testFinished(testSomething2);
63 countDownLatch.countDown();
64 } catch (Exception e) {
65 throw new RuntimeException(e);
66 }
67 }
68 })
69 .start();
70
71 countDownLatch.await();
72 jUnit4TestSetReporter.testFinished(testSomething);
73
74 Assert.assertEquals("Failing tests", 1, reporter.getTestFailed());
75 Assert.assertEquals("Succeeded tests", 1, reporter.getTestSucceeded());
76 }
77
78
79
80
81 public static class STest1 {
82 @Test
83 public void testSomething() {}
84 }
85
86
87
88
89 public static class STest2 {
90 @Test
91 public void testSomething2() {}
92 }
93 }