View Javadoc

1   package org.apache.maven.surefire.junit4;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import junit.framework.TestCase;
22  import junit.framework.TestResult;
23  import org.apache.maven.surefire.common.junit4.JUnit4TestChecker;
24  import org.apache.maven.surefire.testset.TestSetFailedException;
25  import org.junit.Test;
26  import org.junit.internal.runners.InitializationError;
27  import org.junit.runner.Description;
28  import org.junit.runner.RunWith;
29  import org.junit.runner.Runner;
30  import org.junit.runner.notification.RunNotifier;
31  import org.junit.runners.Suite;
32  
33  import static org.junit.Assert.assertFalse;
34  import static org.junit.Assert.assertTrue;
35  
36  /**
37   * @author Kristian Rosenvold
38   */
39  public class JUnit4TestCheckerTest
40  {
41      JUnit4TestChecker jUnit4TestChecker = new JUnit4TestChecker( this.getClass().getClassLoader() );
42  
43      @Test
44      public void validJunit4Annotated()
45          throws TestSetFailedException
46      {
47          assertTrue( jUnit4TestChecker.accept( JUnit4TestCheckerTest.class ) );
48      }
49  
50      @Test
51      public void validJunit4itsAJunit3Test()
52          throws TestSetFailedException
53      {
54          assertTrue( jUnit4TestChecker.accept( AlsoValid.class ) );
55      }
56  
57      @Test
58      public void validJunitSubclassWithoutOwnTestmethods()
59          throws TestSetFailedException
60      {
61          assertTrue( jUnit4TestChecker.accept( SubClassWithoutOwnTestMethods.class ) );
62      }
63  
64      @Test
65      public void validSuite()
66          throws TestSetFailedException
67      {
68          assertTrue( jUnit4TestChecker.accept( SuiteValid1.class ) );
69      }
70  
71      @Test
72      public void validCustomSuite()
73          throws TestSetFailedException
74      {
75          assertTrue( jUnit4TestChecker.accept( SuiteValid2.class ) );
76      }
77  
78      @Test
79      public void validCustomRunner()
80          throws TestSetFailedException
81      {
82          assertTrue( jUnit4TestChecker.accept( SuiteValidCustomRunner.class ) );
83      }
84  
85      @Test
86      public void invalidTest()
87          throws TestSetFailedException
88      {
89          assertFalse( jUnit4TestChecker.accept( NotValidTest.class ) );
90      }
91  
92      @Test
93      public void dontAcceptAbstractClasses()
94      {
95          assertFalse( jUnit4TestChecker.accept( BaseClassWithTest.class ) );
96      }
97  
98      @Test
99      public void suiteOnlyTest()
100     {
101         assertTrue( jUnit4TestChecker.accept( SuiteOnlyTest.class ) );
102     }
103 
104     @Test
105     public void customSuiteOnlyTest()
106     {
107         assertTrue( jUnit4TestChecker.accept( CustomSuiteOnlyTest.class ) );
108     }
109 
110     @Test
111     public void innerClassNotAutomaticallyTc(){
112         assertTrue( jUnit4TestChecker.accept( NestedTC.class));
113         assertFalse( jUnit4TestChecker.accept( NestedTC.Inner.class));
114     }
115 
116 
117     public static class AlsoValid
118         extends TestCase
119     {
120         public void testSomething()
121         {
122 
123         }
124     }
125 
126     public static class SuiteOnlyTest
127     {
128         public static junit.framework.Test suite()
129         {
130             return null;
131         }
132     }
133 
134     public static class CustomSuiteOnlyTest
135     {
136         public static MySuite2 suite()
137         {
138             return null;
139         }
140     }
141 
142     public static class MySuite2
143         implements junit.framework.Test
144     {
145         public int countTestCases()
146         {
147             return 0;
148         }
149 
150         public void run( TestResult testResult )
151         {
152         }
153     }
154 
155 
156     public static class NotValidTest
157     {
158         public void testSomething()
159         {
160         }
161     }
162 
163     public abstract static class BaseClassWithTest
164     {
165         @Test
166         public void weAreAlsoATest()
167         {
168         }
169     }
170 
171     public static class SubClassWithoutOwnTestMethods
172         extends BaseClassWithTest
173     {
174     }
175 
176     @RunWith( Suite.class )
177     public static class SuiteValid1
178     {
179         public void testSomething()
180         {
181 
182         }
183     }
184 
185     class CustomRunner
186         extends Runner
187     {
188         @Override
189         public Description getDescription()
190         {
191             return Description.createSuiteDescription( "CustomRunner" );
192         }
193 
194         @Override
195         public void run( RunNotifier runNotifier )
196         {
197         }
198     }
199 
200     @RunWith( CustomRunner.class )
201     public static class SuiteValidCustomRunner
202     {
203         public void testSomething()
204         {
205 
206         }
207     }
208 
209 
210     @RunWith( MySuite.class )
211     public static class SuiteValid2
212     {
213         public void testSomething()
214         {
215 
216         }
217     }
218 
219     class MySuite
220         extends Suite
221     {
222         MySuite( Class<?> klass )
223             throws InitializationError
224         {
225             super( klass );
226         }
227     }
228 
229     class NestedTC extends TestCase {
230         public class Inner {
231 
232         }
233     }
234 
235 }