Spring 无法识别用org.junit.Test注释的测试

Spring 无法识别用org.junit.Test注释的测试,spring,spring-boot,unit-testing,mockito,powermockito,Spring,Spring Boot,Unit Testing,Mockito,Powermockito,每当我用org.junit.test注释一个测试时,在mvn测试或mvn clean install期间,该测试都不会被识别。但当我用org.junit.jupiter.api.test注释它时,它会被识别; pom.xml 有没有什么方法可以让maven挑选带有org.junit.Test注释的测试 更新:-添加了完整的pom.xml以供参考 我也在使用powermockito来存根我的静态方法。 请告诉我是否可以添加其他内容,以便帮助您调试问题从pom.xml中可以看到,存在两个冗余依赖项。

每当我用org.junit.test注释一个测试时,在mvn测试或mvn clean install期间,该测试都不会被识别。但当我用org.junit.jupiter.api.test注释它时,它会被识别; pom.xml

有没有什么方法可以让maven挑选带有org.junit.Test注释的测试

更新:-添加了完整的pom.xml以供参考 我也在使用powermockito来存根我的静态方法。
请告诉我是否可以添加其他内容,以便帮助您调试问题

从pom.xml中可以看到,存在两个冗余依赖项。之所以选择org.junit.jupiter.api.Test,是因为它是Junit5的一部分,并且作为maven依赖项添加到spring boot starter测试中。基本上,您的问题是您正在使用2个不同版本的junit-4和5

若您想使用JUnit4,请删除dependency-SpringBootStarter测试并尝试一下


请告诉我结果。希望这将有助于进一步调试

我假设您是从IDE运行它,如果是这种情况,请检查您是否在运行配置下选择了Junit5。您可以在此对话框中找到的选项


希望这有帮助,否则请告诉我。

实际上你的问题就在这里

org.springframework.boot 弹簧起动试验 测验 org.junit.vintage 朱尼特老式发动机 通过排除JUnitVintage引擎,您说您的应用程序中没有任何junit4测试用例。但事实并非如此


所以不需要删除整个依赖项。。。。只需删除排除,您的junit4测试用例就会运行。

如果您想同时运行junit4和JUnit5测试,您当前必须这样做:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>junit-4-5</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
        </plugins>
    </build>

</project>
以及测试:

import org.junit.Test;

public class JUnit4Test {
    @Test
    public void test() {

    }
}


import org.junit.jupiter.api.Test;

public class JUnit5Test {
    @Test
    public void test() {

    }
}
在未来版本3.0.0-M5的Surefire和Failsafe中,您将更轻松,无需发动机:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>

包含测试方法的类的名称是什么?PostgresConnectionServiceImpletTest您可以发布完整的pom.xml文件吗?您是否在pom.xml中配置了此maven surefire插件?@RitikaGoel添加了pom.xml。在junit 4中使用starter测试没有问题。@CodeScale默认情况下starter测试使用junit 5,xml既有spring boot starter测试,也有junit 4…这些都是相互冲突的。由于要求使用org.junit.Test编写的测试应该被选中,因此我们只需要junit4出现在类路径上:@DwijrajBhattacharyya完美@RitikaGoel不同意。。。。我重复一遍,你可以同时使用两者。请看我的答案,但删除启动器并不是这里的最佳解决方案。重点是在测试类路径上同时使用junit vintage引擎和junit jupite引擎。重点是在测试类路径上同时使用junit vintage引擎和junit jupite引擎。
import org.junit.Test;

public class JUnit4Test {
    @Test
    public void test() {

    }
}


import org.junit.jupiter.api.Test;

public class JUnit5Test {
    @Test
    public void test() {

    }
}
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>