带有org.junit.Test注释的测试无法识别

问题描述 投票:0回答:4

[每当我用 org.junit.Test 注释测试时,在 mvn test mvn clean install 期间都不会识别该测试。但是当我用org.junit.jupiter.api.Test; 对其进行注释时,该识别被识别。pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.****.it.daas.apibuilder</groupId>
    <artifactId>requestservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Request Service</name>
    <description>Request service for RabbitMQ Message Handler</description>

    <properties>
        <java.version>1.8</java.version>
        <start-class>com.*****.it.daas.apibuilder.submitrequestservice.SubmitRequestApplication</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!-- UNIT Testing libraries -->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.13.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>

<!-- RabbitMQ dependencies -->
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
        </dependency>
<!-- MAILING DEPENDENCIES -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

    <!-- POSTGRES CONNECTION DEPENDENCIES -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
    <!-- HIKARI CP CONNECTION POOLING  -->

        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>2.6.1</version>
        </dependency>
    <!-- JSON OBJECT -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>
    <!-- Google HTTP CLIENT -->
        <dependency>
            <groupId>com.google.http-client</groupId>
            <artifactId>google-http-client</artifactId>
            <version>1.23.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
    <!-- Cloud config dependencies -->

        <dependency>
            <groupId>io.pivotal.spring.cloud</groupId>
            <artifactId>spring-cloud-services-dependencies</artifactId>
            <version>2.1.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>io.pivotal.spring.cloud</groupId>
            <artifactId>spring-cloud-services-starter-config-client</artifactId>
            <version>3.1.2.RELEASE</version>
            <type>pom</type>
        </dependency>

    <!-- Actuators -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    <!-- Security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
    <!-- WEB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

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

        </plugins>
    </build>



</project>

有某种方式,我可以要求Maven进行以org.junit.Test注释的测试吗?

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

spring spring-boot unit-testing mockito powermockito
4个回答
0
投票

据我在pom.xml中看到的,存在几个冗余的依赖项。选择org.junit.jupiter.api.Test的原因是因为它是Junit5的一部分,并作为与spring-boot-starter-test的Maven依赖关系添加。基本上,您的问题是您使用的是junit的2个不同版本-4和5。

如果要使用junit 4,请删除依赖项-spring-boot-starter-test,然后尝试一下。

请让我知道结果。希望这将有助于进一步调试!


1
投票

实际上您的问题在这里

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

排除junit-vintage-engine 您说您的应用程序中没有任何junit4测试用例] >>。但事实并非如此。...

因此无需删除整个依赖项。...只需删除排除项,您的junit4测试用例就会运行。


0
投票

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


0
投票

如果要同时运行JUnit4和JUnit5测试,则当前必须这样做:

© www.soinside.com 2019 - 2024. All rights reserved.