在 Spring 引导 2.6.2 迁移 后 , Junit ( 4.12 ) 未 执行

nkoocmlb  于 2022-11-11  发布在  Spring
关注(0)|答案(4)|浏览(166)

我已经从Spring迁移到了Spring-boot版本2.6.2。mvn干净安装成功了,但是没有junit(版本4.12)在执行。经过一些研究,我知道Spring-boot 2.4以后,JUnit 4已经被删除了。我尝试了以下解决方案,但没有成功。
After updating to latest Spring boot version, spring-boot-starter-parent 2.6.2, my tests stop executing
Spring Boot maven unit tests not being executed

lsmepo6l

lsmepo6l1#

JUnit 5 = JUnit平台+ JUnit Jupiter + JUnit版本。

请参阅:Junit 5文档
如果你通过依赖最新的spring-boot-starter-test来导入junit-jupiter,它将只运行Junit 5风格的测试用例。所以Junit提供了junit-vintage来运行旧的Junit 3/Junit 4用例,默认情况下它不包含在spring-boot-starter-test依赖项中。所以有两个解决方案来保持Junit 4的运行:
1.依赖于junit-jupiter和junit-vintage来支持所有junit 3/4/5的情况。

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
 </dependency>
 <dependency>
     <groupId>org.junit.vintage</groupId>
     <artifactId>junit-vintage-engine</artifactId>
     <scope>test</scope>
 </dependency>

1.从spring-boot-starter-test中排除junit-jupiter以运行Junit 4

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
     <exclusions>
         <exclusion>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter</artifactId>
         </exclusion>
     </exclusions>
 </dependency>
zi8p0yeb

zi8p0yeb2#

从spring-boot-starter-test依赖项中排除junit-jupiter-engine和junit-vintage-engine,然后在pom中添加JUnit 4依赖项:

<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
u91tlkcl

u91tlkcl3#

我认为您应该迁移到JUnit 5。

sxissh06

sxissh064#

从SpringBoot V2.2开始,Spring默认只支持JUnit 5,并删除了与JUnit 4的向后兼容性。在Spring 2.2发行说明中明确指出:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.2-Release-Notes
“您不能使用junit-vintage-engine,并且需要显式回滚到JUnit 4:“

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-junit-jupiter</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

Springboot 2.4发行说明提到了POM依赖项的更改:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.4-Release-Notes#junit-5s-vintage-engine-removed-from-spring-boot-starter-test

相关问题