View Javadoc

1   package org.apache.maven.surefire.junitcore;
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.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  import org.apache.maven.surefire.junit4.MockReporter;
26  
27  import junit.framework.TestCase;
28  import org.junit.Assume;
29  import org.junit.Test;
30  import org.junit.runner.Computer;
31  import org.junit.runner.Description;
32  import org.junit.runner.JUnitCore;
33  import org.junit.runner.Result;
34  import org.junit.runner.notification.RunListener;
35  
36  /**
37   * @author Kristian Rosenvold
38   */
39  public class JUnitCoreRunListenerTest
40      extends TestCase
41  {
42      public void testTestRunStarted()
43          throws Exception
44      {
45          RunListener jUnit4TestSetReporter =
46              new JUnitCoreRunListener( new MockReporter(), new HashMap<String, TestSet>() );
47          JUnitCore core = new JUnitCore();
48          core.addListener( jUnit4TestSetReporter );
49          Result result = core.run( new Computer(), STest1.class, STest2.class );
50          core.removeListener( jUnit4TestSetReporter );
51          assertEquals( 2, result.getRunCount() );
52      }
53  
54      public void testFailedAssumption()
55          throws Exception
56      {
57          RunListener jUnit4TestSetReporter =
58              new JUnitCoreRunListener( new MockReporter(), new HashMap<String, TestSet>() );
59          JUnitCore core = new JUnitCore();
60          core.addListener( jUnit4TestSetReporter );
61          Result result = core.run( new Computer(), TestWithAssumptionFailure.class );
62          core.removeListener( jUnit4TestSetReporter );
63          assertEquals( 1, result.getRunCount() );
64      }
65  
66      public void testStateForClassesWithNoChildren()
67          throws Exception
68      {
69          Description testDescription =
70              Description.createSuiteDescription( "testMethod(cannot.be.loaded.by.junit.Test)" );
71          Description st1 = Description.createSuiteDescription( STest1.class);
72  //        st1.addChild( Description.createSuiteDescription( STest1.class ) );
73          testDescription.addChild( st1 );
74          Description st2 = Description.createSuiteDescription( STest2.class);
75    //      st2.addChild( Description.createSuiteDescription( STest2.class ) );
76          testDescription.addChild( st2 );
77  
78          Map<String, TestSet> classMethodCounts = new HashMap<String, TestSet>();
79          JUnitCoreRunListener listener = new JUnitCoreRunListener( new MockReporter(), classMethodCounts );
80          listener.testRunStarted( testDescription );
81          assertEquals( 2, classMethodCounts.size() );
82          Iterator<TestSet> iterator = classMethodCounts.values().iterator();
83          assertFalse(iterator.next().equals( iterator.next() ));
84  
85      }
86  
87      public void testTestClassNotLoadableFromJUnitClassLoader()
88          throws Exception
89      {
90          // can't use Description.createTestDescription() methods as these require a loaded Class
91          Description testDescription =
92              Description.createSuiteDescription( "testMethod(cannot.be.loaded.by.junit.Test)" );
93          assertEquals( "testMethod", testDescription.getMethodName() );
94          assertEquals( "cannot.be.loaded.by.junit.Test", testDescription.getClassName() );
95          // assert that the test class is not visible by the JUnit classloader
96          assertNull( testDescription.getTestClass() );
97          Description suiteDescription = Description.createSuiteDescription( "testSuite" );
98          suiteDescription.addChild( testDescription );
99          Map<String, TestSet> classMethodCounts = new HashMap<String, TestSet>();
100         JUnitCoreRunListener listener = new JUnitCoreRunListener( new MockReporter(), classMethodCounts );
101         listener.testRunStarted( suiteDescription );
102         assertEquals( 1, classMethodCounts.size() );
103         TestSet testSet = classMethodCounts.get( "cannot.be.loaded.by.junit.Test" );
104         assertNotNull( testSet );
105     }
106 
107     public static class STest1
108     {
109         @Test
110         public void testSomething()
111         {
112         }
113     }
114 
115     public static class STest2
116     {
117         @Test
118         public void testSomething2()
119         {
120         }
121     }
122 
123     public static class TestWithAssumptionFailure
124     {
125         @Test
126         public void testSomething2()
127         {
128             Assume.assumeTrue( false );
129         }
130     }
131 
132 }