View Javadoc

1   package org.apache.maven.surefire.common.junit4;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Kristian Rosenvold
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 }