即使在 pow.xml 中添加依赖项后 junit 也不会出现

junit does not appear even after adding the dependency in pow.xml

我正在使用 Spring 引导学习 Java,但我无法使 JUnit 注释在我的 类 中工作。

当我输入 @RunWith(SpringRunner.class)@ExtendWith(SpringExtension.class) 时它不会'甚至出现 Cannot solve symbol 就好像我没有在 pom.xml.

中添加依赖一样

我满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.4.4</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <groupId>com.fquadros</groupId>
    <artifactId>minhasfinancas</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>minhasfinancas</name>
    <description>Projeto para gerenciamento de finanças pessoais</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <!-- Dependencia para poder rodar testes feitos em Junit 4 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>2.2</version>
        </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>2.22.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>
</project>

我正在使用 IDE: Intelij 2018.2.8

我尝试做的事情:

添加依赖项后,我去构建/重建项目。

在终端中,我已经 运行 mvn cleanmvn install 命令。

在安装时出现几个错误:

https://codepen.io/flavionfg/full/KKaQVbG

我还需要做任何其他事情才能使用 JUnit 吗?

而不是 运行先 mvn clean 然后 mvn install,尝试先 运行 mvn clean compile 下载依赖项并编译你的项目。

另外要提的是,如果您计划支持 JUnit 4 测试,您应该添加 junit-vintage-engine 依赖项,因为 junit-jupiter-engine 用于 运行ning JUnit 5 测试。

您可以在这里阅读更多内容:https://junit.org/junit5/docs/current/user-guide/#overview-what-is-junit-5