View Javadoc
1   package org.apache.maven.surefire.junitplatform;
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 static org.apache.maven.surefire.testset.TestListResolver.toClassFileName;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  import java.lang.reflect.Method;
29  
30  import org.apache.maven.surefire.testset.TestListResolver;
31  import org.junit.Test;
32  import org.junit.jupiter.engine.descriptor.ClassTestDescriptor;
33  import org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor;
34  import org.junit.platform.engine.ConfigurationParameters;
35  import org.junit.platform.engine.FilterResult;
36  import org.junit.platform.engine.UniqueId;
37  
38  /**
39   * Unit tests for {@link TestMethodFilter}.
40   *
41   * @since 2.22.0
42   */
43  public class TestMethodFilterTest
44  {
45      private static final ConfigurationParameters CONFIG_PARAMS = mock(ConfigurationParameters.class);
46  
47      private final TestListResolver resolver = mock( TestListResolver.class );
48  
49      private final TestMethodFilter filter = new TestMethodFilter( this.resolver );
50  
51      @Test
52      public void includesBasedOnTestListResolver()
53                      throws Exception
54      {
55          when( resolver.shouldRun( toClassFileName( TestClass.class ), "testMethod" ) ).thenReturn( true );
56  
57          FilterResult result = filter.apply( newTestMethodDescriptor() );
58  
59          assertTrue( result.included() );
60          assertFalse( result.excluded() );
61      }
62  
63      @Test
64      public void excludesBasedOnTestListResolver()
65                      throws Exception
66      {
67          when( resolver.shouldRun( toClassFileName( TestClass.class ), "testMethod" ) ).thenReturn( false );
68  
69          FilterResult result = filter.apply( newTestMethodDescriptor() );
70  
71          assertFalse( result.included() );
72          assertTrue( result.excluded() );
73      }
74  
75      @Test
76      public void includesTestDescriptorWithClassSource()
77      {
78          FilterResult result = filter.apply( newClassTestDescriptor() );
79  
80          assertTrue( result.included() );
81          assertFalse( result.excluded() );
82      }
83  
84      private static TestMethodTestDescriptor newTestMethodDescriptor()
85                      throws Exception
86      {
87          UniqueId uniqueId = UniqueId.forEngine( "method" );
88          Class<TestClass> testClass = TestClass.class;
89          Method testMethod = testClass.getMethod( "testMethod" );
90          return new TestMethodTestDescriptor( uniqueId, testClass, testMethod );
91      }
92  
93      private static ClassTestDescriptor newClassTestDescriptor()
94      {
95          UniqueId uniqueId = UniqueId.forEngine( "class" );
96          return new ClassTestDescriptor( uniqueId, TestClass.class, CONFIG_PARAMS );
97      }
98  
99      public static class TestClass
100     {
101         public void testMethod()
102         {
103         }
104     }
105 }