Skip to content

Commit

Permalink
Consider JUnit Jupiter test classes in TestTypeExcludeFilter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wilkinsona committed Sep 21, 2017
1 parent e9147c2 commit 7c1bc68
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 5 deletions.
5 changes: 5 additions & 0 deletions spring-boot-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* 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.
Expand Down Expand Up @@ -28,12 +28,15 @@
* 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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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() {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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() {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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
}));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* 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.
Expand Down Expand Up @@ -31,6 +31,7 @@
* Tests for {@link TestTypeExcludeFilter}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class TestTypeExcludeFilterTests {

Expand All @@ -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),
Expand All @@ -58,6 +77,15 @@ public void matchesNestedConfigurationClassWithoutTestMethodsIfItHasRunWith()
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),
Expand Down

0 comments on commit 7c1bc68

Please sign in to comment.