本文介绍了Spring Boot和JUnit 5之间的交互-必须使用整体工件,而不是个体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已被问过(例如,此处),但是我的观察与以前的报道不同.

This question has been asked before (e.g. here), but my observation was not the same as those previously reported.

我注意到要使JUnit 5正常工作,我必须包括整个JUnit 5工件

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')

如果我改为包含各个工件,那么将不会进行JUnit测试

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?

(我还在非Spring-Boot项目中尝试过此方法-在那种情况下,可以包含单个工件.这引起了很多混乱.)

(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.)

我在这里用gradle显示结果,但是我在maven中也得到了类似的结果.

Here I am showing the result with gradle, but I have had similar result with maven too.

我正在使用Gradle 5.4.1Spring Boot 2.1.7.RELEASEJUnit 5.5.1

我包括完整的build.gradle和下面的测试课程

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);
    }

}

请注意,在这种情况下,我希望测试方法failMe()中会引发异常-以便证明测试方法已被跑步者采用,并且未被默默忽略.

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.

推荐答案

感谢@johanneslink的提示(在开头的问题的评论中),现在我认为我对问题的理解更好:

Thanks for the hint from @johanneslink (in the comment of the opening question), now I think I understand better the issues:

最好使用聚合工件

testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')

如果您真的要使用单个工件,请确保它们的版本兼容

此组合将有效

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')

但不是这个

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')

(其他三个工件不相关,因此我在这里省略了它们.例如,根据《 JUnit 5用户指南》

(The other three artifacts are not relevant so I am omitting them here. For example, according to the JUnit 5 User Guide

用于在JUnit 4环境中的JUnit平台上执行测试和测试套件的运行器.

Runner for executing tests and test suites on the JUnit Platform in a JUnit 4 environment.

,与此处无关.

这篇关于Spring Boot和JUnit 5之间的交互-必须使用整体工件,而不是个体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:34