问题
This question has been asked before (e.g. here), but my observation was not the same as those previously reported.
I noticed that to get JUnit 5 to work, I must include the overall JUnit 5 artifact
testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
If I, instead, included the individual artifacts, then the JUnit test would not be picked up
testImplementation('org.junit.platform:junit-platform-runner:1.3.1')
testImplementation('org.junit.platform:junit-platform-launcher:1.0.0')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-params:5.5.1')
testImplementation('org.junit.vintage:junit-vintage-engine:5.5.1')
Has anyone seen something similar before?
(I also tried this with a non-Spring-Boot project -- in that case it was okay to include the individual artifacts. This was causing a lot of confusion.)
Here I am showing the result with gradle, but I have had similar result with maven too.
I am using Gradle 5.4.1
, Spring Boot 2.1.7.RELEASE
, and JUnit 5.5.1
I am including the full build.gradle
and the test class below
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
// testImplementation('org.junit.platform:junit-platform-runner:1.3.1')
// testImplementation('org.junit.platform:junit-platform-launcher:1.0.0')
// testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
// testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')
// testImplementation('org.junit.jupiter:junit-jupiter-params:5.5.1')
// testImplementation('org.junit.vintage:junit-vintage-engine:5.5.1')
}
test {
useJUnitPlatform()
}
DemoApplicationTest.java
package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class DemoApplicationTests {
@Test
public void failMe() {
Assertions.assertTrue(Boolean.FALSE);
}
}
Note that in this case I was expecting an exception be thrown in the test method failMe()
-- so as to prove that the test method had been picked up by the runner and was not silently ignored.
回答1:
Thanks for the hint from @johanneslink (in the comment of the opening question), now I think I understand better the issues:
It is better to use the aggregate artifact
testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
If you really want to use the individual artifacts, make sure their versions are compatible
This combination would work
testImplementation('org.junit.platform:junit-platform-launcher:1.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')
But not this
testImplementation('org.junit.platform:junit-platform-launcher:1.3.1')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')
(The other three artifacts are not relevant so I am omitting them here. For example, according to the JUnit 5 User Guide
junit-platform-runner
Runner for executing tests and test suites on the JUnit Platform in a JUnit 4 environment.
and is not relevant here.)
来源:https://stackoverflow.com/questions/57434659/interaction-between-spring-boot-and-junit-5-must-use-the-overall-artifacts-no