Skip to content

Commit 7c1bc68

Browse files
committed
Consider JUnit Jupiter test classes in TestTypeExcludeFilter
Previously, TestTypeExcludeFilter only looked for JUnit 4 class and method annotations when determining if a class was a test class. As a result, if an application was using JUnit Jupiter, its test classes would not be exluded during component scanning. This commit expands TestTypeExcludeFilter to also identify classes using JUnit Jupiter. This includes classes (meta-)annotated with @ExtendWith and methods (meta-)annotated with @testable. The later provides detection of Jupiter's @test, @testfactory, and @RepeatedTest annotations all of which are meta-annotated with @testable. Closes gh-6898
1 parent e9147c2 commit 7c1bc68

File tree

7 files changed

+164
-5
lines changed

7 files changed

+164
-5
lines changed

spring-boot-test/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@
167167
<artifactId>spring-webmvc</artifactId>
168168
<scope>test</scope>
169169
</dependency>
170+
<dependency>
171+
<groupId>org.junit.jupiter</groupId>
172+
<artifactId>junit-jupiter-api</artifactId>
173+
<scope>test</scope>
174+
</dependency>
170175
</dependencies>
171176
<build>
172177
<plugins>

spring-boot-test/src/main/java/org/springframework/boot/test/context/filter/TestTypeExcludeFilter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,12 +28,15 @@
2828
* well as inner-classes of tests.
2929
*
3030
* @author Phillip Webb
31+
* @author Andy Wilkinson
3132
*/
3233
class TestTypeExcludeFilter extends TypeExcludeFilter {
3334

34-
private static final String[] CLASS_ANNOTATIONS = { "org.junit.runner.RunWith" };
35+
private static final String[] CLASS_ANNOTATIONS = { "org.junit.runner.RunWith",
36+
"org.junit.jupiter.api.extension.ExtendWith" };
3537

36-
private static final String[] METHOD_ANNOTATIONS = { "org.junit.Test" };
38+
private static final String[] METHOD_ANNOTATIONS = { "org.junit.Test",
39+
"org.junit.platform.commons.annotation.Testable", };
3740

3841
@Override
3942
public boolean match(MetadataReader metadataReader,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context.filter;
18+
19+
import org.junit.jupiter.api.extension.ExtendWith;
20+
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.test.context.junit.jupiter.SpringExtension;
23+
24+
@ExtendWith(SpringExtension.class)
25+
public abstract class AbstractJupiterTestWithConfigAndExtendWith {
26+
27+
@Configuration
28+
static class Config {
29+
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context.filter;
18+
19+
import org.junit.jupiter.api.RepeatedTest;
20+
21+
public class JupiterRepeatedTestExample {
22+
23+
@RepeatedTest(5)
24+
public void repeatedTest() {
25+
26+
}
27+
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context.filter;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
public class JupiterTestExample {
22+
23+
@Test
24+
public void repeatedTest() {
25+
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context.filter;
18+
19+
import java.util.Arrays;
20+
import java.util.Collection;
21+
22+
import org.junit.jupiter.api.DynamicNode;
23+
import org.junit.jupiter.api.DynamicTest;
24+
import org.junit.jupiter.api.TestFactory;
25+
26+
public class JupiterTestFactoryExample {
27+
28+
@TestFactory
29+
public Collection<DynamicNode> testFactory() {
30+
return Arrays.asList(DynamicTest.dynamicTest("Some dynamic test", () -> {
31+
// Test
32+
}));
33+
}
34+
35+
}

spring-boot-test/src/test/java/org/springframework/boot/test/context/filter/TestTypeExcludeFilterTests.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
* Tests for {@link TestTypeExcludeFilter}.
3232
*
3333
* @author Phillip Webb
34+
* @author Andy Wilkinson
3435
*/
3536
public class TestTypeExcludeFilterTests {
3637

@@ -39,11 +40,29 @@ public class TestTypeExcludeFilterTests {
3940
private MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
4041

4142
@Test
42-
public void matchesTestClass() throws Exception {
43+
public void matchesJUnit4TestClass() throws Exception {
4344
assertThat(this.filter.match(getMetadataReader(TestTypeExcludeFilterTests.class),
4445
this.metadataReaderFactory)).isTrue();
4546
}
4647

48+
@Test
49+
public void matchesJUnitJupiterTestClass() throws Exception {
50+
assertThat(this.filter.match(getMetadataReader(JupiterTestExample.class),
51+
this.metadataReaderFactory)).isTrue();
52+
}
53+
54+
@Test
55+
public void matchesJUnitJupiterRepeatedTestClass() throws Exception {
56+
assertThat(this.filter.match(getMetadataReader(JupiterRepeatedTestExample.class),
57+
this.metadataReaderFactory)).isTrue();
58+
}
59+
60+
@Test
61+
public void matchesJUnitJupiterTestFactoryClass() throws Exception {
62+
assertThat(this.filter.match(getMetadataReader(JupiterTestFactoryExample.class),
63+
this.metadataReaderFactory)).isTrue();
64+
}
65+
4766
@Test
4867
public void matchesNestedConfiguration() throws Exception {
4968
assertThat(this.filter.match(getMetadataReader(NestedConfig.class),
@@ -58,6 +77,15 @@ public void matchesNestedConfigurationClassWithoutTestMethodsIfItHasRunWith()
5877
this.metadataReaderFactory)).isTrue();
5978
}
6079

80+
@Test
81+
public void matchesNestedConfigurationClassWithoutTestMethodsIfItHasExtendWith()
82+
throws Exception {
83+
assertThat(this.filter.match(
84+
getMetadataReader(
85+
AbstractJupiterTestWithConfigAndExtendWith.Config.class),
86+
this.metadataReaderFactory)).isTrue();
87+
}
88+
6189
@Test
6290
public void matchesTestConfiguration() throws Exception {
6391
assertThat(this.filter.match(getMetadataReader(SampleTestConfig.class),

0 commit comments

Comments
 (0)