View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.junitplatform;
20  
21  import java.util.List;
22  import java.util.stream.Stream;
23  
24  import org.junit.Test;
25  import org.junit.jupiter.api.DynamicTest;
26  import org.junit.jupiter.api.Nested;
27  import org.junit.jupiter.api.TestFactory;
28  import org.junit.platform.engine.Filter;
29  import org.junit.platform.launcher.core.LauncherFactory;
30  
31  import static java.util.Collections.emptyList;
32  import static org.junit.jupiter.api.Assertions.assertFalse;
33  import static org.junit.jupiter.api.Assertions.assertTrue;
34  
35  /**
36   * Unit tests for {@link TestPlanScannerFilter}.
37   *
38   * @since 2.22.0
39   */
40  public class TestPlanScannerFilterTest {
41  
42      @Test
43      public void emptyClassIsNotAccepted() {
44          assertFalse(newFilter().accept(EmptyClass.class), "does not accept empty class");
45      }
46  
47      @Test
48      public void classWithNoTestMethodsIsNotAccepted() {
49          assertFalse(newFilter().accept(ClassWithMethods.class), "does not accept class with no @Test methods");
50      }
51  
52      @Test
53      public void classWithTestMethodsIsAccepted() {
54          assertTrue(newFilter().accept(ClassWithTestMethods.class));
55      }
56  
57      @Test
58      public void classWithNestedTestClassIsAccepted() {
59          assertTrue(newFilter().accept(ClassWithNestedTestClass.class));
60      }
61  
62      @Test
63      public void classWithDeeplyNestedTestClassIsAccepted() {
64          assertTrue(newFilter().accept(ClassWithDeeplyNestedTestClass.class));
65      }
66  
67      @Test
68      public void classWithTestFactoryIsAccepted() {
69          assertTrue(newFilter().accept(ClassWithTestFactory.class));
70      }
71  
72      @Test
73      public void classWithNestedTestFactoryIsAccepted() {
74          assertTrue(newFilter().accept(ClassWithNestedTestFactory.class));
75      }
76  
77      private static TestPlanScannerFilter newFilter() {
78          return new TestPlanScannerFilter(LauncherFactory.create(), new Filter<?>[0]);
79      }
80  
81      static class EmptyClass {}
82  
83      static class ClassWithMethods {
84  
85          void method1() {}
86  
87          void method2() {}
88      }
89  
90      static class ClassWithTestMethods {
91  
92          @Test
93          void test1() {}
94  
95          @org.junit.jupiter.api.Test
96          public void test2() {}
97      }
98  
99      static class ClassWithNestedTestClass {
100 
101         void method() {}
102 
103         @Nested
104         class TestClass {
105 
106             @org.junit.jupiter.api.Test
107             void test1() {}
108         }
109     }
110 
111     static class ClassWithDeeplyNestedTestClass {
112 
113         @Nested
114         class Level1 {
115 
116             @Nested
117             class Level2 {
118 
119                 @Nested
120                 class TestClass {
121 
122                     @org.junit.jupiter.api.Test
123                     void test1() {}
124                 }
125             }
126         }
127     }
128 
129     static class ClassWithTestFactory {
130 
131         @TestFactory
132         Stream<DynamicTest> tests() {
133             return Stream.empty();
134         }
135     }
136 
137     static class ClassWithNestedTestFactory {
138 
139         @Nested
140         class TestClass {
141 
142             @TestFactory
143             List<DynamicTest> tests() {
144                 return emptyList();
145             }
146         }
147     }
148 }