Commit 7c1bc68f authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent e9147c2f
......@@ -167,6 +167,11 @@
<artifactId>spring-webmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
......
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -28,12 +28,15 @@ import org.springframework.core.type.classreading.MetadataReaderFactory;
* well as inner-classes of tests.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
class TestTypeExcludeFilter extends TypeExcludeFilter {
private static final String[] CLASS_ANNOTATIONS = { "org.junit.runner.RunWith" };
private static final String[] CLASS_ANNOTATIONS = { "org.junit.runner.RunWith",
"org.junit.jupiter.api.extension.ExtendWith" };
private static final String[] METHOD_ANNOTATIONS = { "org.junit.Test" };
private static final String[] METHOD_ANNOTATIONS = { "org.junit.Test",
"org.junit.platform.commons.annotation.Testable", };
@Override
public boolean match(MetadataReader metadataReader,
......
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.context.filter;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
public abstract class AbstractJupiterTestWithConfigAndExtendWith {
@Configuration
static class Config {
}
}
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.context.filter;
import org.junit.jupiter.api.RepeatedTest;
public class JupiterRepeatedTestExample {
@RepeatedTest(5)
public void repeatedTest() {
}
}
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.context.filter;
import org.junit.jupiter.api.Test;
public class JupiterTestExample {
@Test
public void repeatedTest() {
}
}
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.context.filter;
import java.util.Arrays;
import java.util.Collection;
import org.junit.jupiter.api.DynamicNode;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
public class JupiterTestFactoryExample {
@TestFactory
public Collection<DynamicNode> testFactory() {
return Arrays.asList(DynamicTest.dynamicTest("Some dynamic test", () -> {
// Test
}));
}
}
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -31,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link TestTypeExcludeFilter}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class TestTypeExcludeFilterTests {
......@@ -39,11 +40,29 @@ public class TestTypeExcludeFilterTests {
private MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
@Test
public void matchesTestClass() throws Exception {
public void matchesJUnit4TestClass() throws Exception {
assertThat(this.filter.match(getMetadataReader(TestTypeExcludeFilterTests.class),
this.metadataReaderFactory)).isTrue();
}
@Test
public void matchesJUnitJupiterTestClass() throws Exception {
assertThat(this.filter.match(getMetadataReader(JupiterTestExample.class),
this.metadataReaderFactory)).isTrue();
}
@Test
public void matchesJUnitJupiterRepeatedTestClass() throws Exception {
assertThat(this.filter.match(getMetadataReader(JupiterRepeatedTestExample.class),
this.metadataReaderFactory)).isTrue();
}
@Test
public void matchesJUnitJupiterTestFactoryClass() throws Exception {
assertThat(this.filter.match(getMetadataReader(JupiterTestFactoryExample.class),
this.metadataReaderFactory)).isTrue();
}
@Test
public void matchesNestedConfiguration() throws Exception {
assertThat(this.filter.match(getMetadataReader(NestedConfig.class),
......@@ -58,6 +77,15 @@ public class TestTypeExcludeFilterTests {
this.metadataReaderFactory)).isTrue();
}
@Test
public void matchesNestedConfigurationClassWithoutTestMethodsIfItHasExtendWith()
throws Exception {
assertThat(this.filter.match(
getMetadataReader(
AbstractJupiterTestWithConfigAndExtendWith.Config.class),
this.metadataReaderFactory)).isTrue();
}
@Test
public void matchesTestConfiguration() throws Exception {
assertThat(this.filter.match(getMetadataReader(SampleTestConfig.class),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment