Remove JUnit 5's vintage engine from spring-boot-starter-test

Closes gh-21625
This commit is contained in:
Andy Wilkinson 2020-06-05 13:58:42 +01:00
parent 721399bdc4
commit 8fb7a6cace
7 changed files with 13 additions and 141 deletions

View File

@ -6251,32 +6251,34 @@ Most developers use the `spring-boot-starter-test` "`Starter`", which imports bo
[TIP]
====
The starter also brings the vintage engine so that you can run both JUnit 4 and JUnit 5 tests.
If you have migrated your tests to JUnit 5, you should exclude JUnit 4 support, as shown in the following example:
If you have tests that use JUnit 4, JUnit 5's vintage engine can be used to run them.
To use the vintage engine, add a dependency on `junit-vintage-engine`, as shown in the following example:
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
----
====
`hamcrest-core` is excluded in favor of `org.hamcrest:hamcrest` that is part of `spring-boot-starter-test`.
[[boot-features-test-scope-dependencies]]
=== Test Scope Dependencies
The `spring-boot-starter-test` "`Starter`" (in the `test` `scope`) contains the following provided libraries:
* https://junit.org/junit5/[JUnit 5] (including the vintage engine for backward compatibility with JUnit 4): The de-facto standard for unit testing Java applications.
* https://junit.org/junit5/[JUnit 5]: The de-facto standard for unit testing Java applications.
* {spring-framework-docs}/testing.html#integration-testing[Spring Test] & Spring Boot Test: Utilities and integration test support for Spring Boot applications.
* https://assertj.github.io/doc/[AssertJ]: A fluent assertion library.
* https://github.com/hamcrest/JavaHamcrest[Hamcrest]: A library of matcher objects (also known as constraints or predicates).
@ -8100,10 +8102,6 @@ While it is possible to use JUnit 4 to test Kotlin code, JUnit 5 is provided by
JUnit 5 enables a test class to be instantiated once and reused for all of the class's tests.
This makes it possible to use `@BeforeClass` and `@AfterClass` annotations on non-static methods, which is a good fit for Kotlin.
JUnit 5 is the default and the vintage engine is provided for backward compatibility with JUnit 4.
If you don't use it, exclude `org.junit.vintage:junit-vintage-engine`.
You also need to {junit5-docs}/#writing-tests-test-instance-lifecycle-changing-default[switch test instance lifecycle to "per-class"].
To mock Kotlin classes, https://mockk.io/[MockK] is recommended.
If you need the `Mockk` equivalent of the Mockito specific <<boot-features-testing-spring-boot-applications-mocking-beans,`@MockBean` and `@SpyBean` annotations>>, you can use https://github.com/Ninja-Squad/springmockk[SpringMockK] which provides similar `@MockkBean` and `@SpykBean` annotations.

View File

@ -2,7 +2,7 @@ plugins {
id "org.springframework.boot.starter"
}
description = "Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito"
description = "Starter for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest and Mockito"
dependencies {
api(platform(project(":spring-boot-project:spring-boot-dependencies")))
@ -14,9 +14,6 @@ dependencies {
api("org.assertj:assertj-core")
api("org.hamcrest:hamcrest")
api("org.junit.jupiter:junit-jupiter")
api("org.junit.vintage:junit-vintage-engine") {
exclude group: "org.hamcrest", module: "hamcrest-core"
}
api("org.mockito:mockito-core")
api("org.mockito:mockito-junit-jupiter")
api("org.skyscreamer:jsonassert")

View File

@ -1,26 +0,0 @@
plugins {
id "java"
id "org.springframework.boot.conventions"
}
description = "Spring Boot JUnit Jupiter smoke test"
dependencies {
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) {
exclude group: "org.junit.vintage"
}
}
test {
testLogging {
afterSuite { description, result ->
if (!description.parent) {
if (!result.testCount) {
throw new GradleException("No tests were executed")
}
}
}
}
}

View File

@ -1,30 +0,0 @@
/*
* Copyright 2012-2019 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
*
* https://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 smoketest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MessageController {
@GetMapping("/hi")
public String hello() {
return "Hello World";
}
}

View File

@ -1,29 +0,0 @@
/*
* Copyright 2012-2019 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
*
* https://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 smoketest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleJUnitJupiterApplication {
public static void main(String[] args) {
SpringApplication.run(SampleJUnitJupiterApplication.class, args);
}
}

View File

@ -1,40 +0,0 @@
/*
* Copyright 2012-2019 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
*
* https://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 smoketest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class SampleJUnitJupiterApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@Test
void testMessage() {
String message = this.restTemplate.getForObject("/hi", String.class);
assertThat(message).isEqualTo("Hello World");
}
}

View File

@ -7,8 +7,10 @@ description = "Spring Boot JUnit Vintage smoke test"
dependencies {
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
testImplementation("org.junit.vintage:junit-vintage-engine") {
exclude group: "org.hamcrest", module: "hamcrest-core"
}
}
test {