1 package org.apache.maven.surefire.common.junit4;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.concurrent.CountDownLatch;
23 import org.apache.maven.surefire.junit4.MockReporter;
24
25 import junit.framework.Assert;
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
38 {
39 @Test
40 public void testTestStarted()
41 throws Exception
42 {
43 RunListener jUnit4TestSetReporter = new JUnit4RunListener( new MockReporter() );
44 Runner junitTestRunner = Request.classes( "abc", STest1.class, STest2.class ).getRunner();
45 RunNotifier runNotifier = new RunNotifier();
46 runNotifier.addListener( jUnit4TestSetReporter );
47 junitTestRunner.run( runNotifier );
48 }
49
50 @Test
51 public void testParallelInvocations()
52 throws Exception
53 {
54 final MockReporter reporter = new MockReporter();
55 final RunListener jUnit4TestSetReporter = new JUnit4RunListener( reporter );
56 final CountDownLatch countDownLatch = new CountDownLatch( 1 );
57 final Description testSomething = Description.createTestDescription( STest1.class, "testSomething" );
58 final Description testSomething2 = Description.createTestDescription( STest2.class, "testSomething2" );
59
60 jUnit4TestSetReporter.testStarted( testSomething );
61
62 new Thread( new Runnable()
63 {
64 public void run()
65 {
66 try
67 {
68 jUnit4TestSetReporter.testStarted( testSomething2 );
69 jUnit4TestSetReporter.testFailure( new Failure( testSomething2, new AssertionError( "Fud" ) ) );
70 jUnit4TestSetReporter.testFinished( testSomething2 );
71 countDownLatch.countDown();
72 }
73 catch ( Exception e )
74 {
75 throw new RuntimeException( e );
76 }
77 }
78 } ).start();
79
80 countDownLatch.await();
81 jUnit4TestSetReporter.testFinished( testSomething );
82
83 Assert.assertEquals( "Failing tests", 1, reporter.getTestFailed() );
84 Assert.assertEquals( "Succeeded tests", 1, reporter.getTestSucceeded() );
85 }
86
87
88 public static class STest1
89 {
90 @Test
91 public void testSomething()
92 {
93
94 }
95 }
96
97 public static class STest2
98 {
99 @Test
100 public void testSomething2()
101 {
102
103 }
104 }
105
106 }