简体   繁体   English

JUnit5:导入断言时遇到问题

[英]JUnit5: Trouble Importing Assertions

I'm trying to use JUnit5 to create some basic unit tests.我正在尝试使用 JUnit5 创建一些基本的单元测试。 I go to my Analyzer.java class and get the popup for creating a test.我转到我的Analyzer.java类并获取用于创建测试的弹出窗口。 I hit Create New Test , setting Testing Library to JUnit5.我点击Create New Test ,将测试库设置为 JUnit5。 I check off a bunch of methods to generate test methods for and hit OK.我检查了一堆方法来生成测试方法并点击确定。

So now I have an AnalyzerTest.java file and at the top I have:所以现在我有一个AnalyzerTest.java文件,在顶部我有:

import static org.junit.jupiter.api.Assertions.*;

Unfortunately, Assertions is red (this is in IntelliJ IDEA).不幸的是, Assertions是红色的(这是在 IntelliJ IDEA 中)。 When I hover, it says "Cannot find symbol Assertions".当我悬停时,它说“找不到符号断言”。 In a similar vein, I have:同样,我有:

 @org.junit.jupiter.api.Test

before each test method and when I hover, I get "Cannot resolve symbol Test"在每个测试方法之前以及当我悬停时,我得到“无法解析符号测试”

I simply want to create and then run some unit tests but obviously am doing something wrong.我只是想创建然后运行一些单元测试,但显然我做错了什么。

Any ideas?有任何想法吗?

Thanks!谢谢!

Gradle摇篮

Add the following dependency to your Gradle:将以下依赖项添加到您的 Gradle:

 testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.1")

Under your dependencies.在您的依赖项下。

    dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.1")

I don't know you are using or maven or gradle.我不知道你在使用 Maven 还是 gradle。 But if you are using maven just add below dependecies in between your tags.但是,如果您使用的是 maven,只需在标签之间添加以下依赖项。 Let me know if you need more help regarding this.如果您在这方面需要更多帮助,请告诉我。 I have used older version below, you can check the latest version from https://mvnrepository.com and update the pom script.我在下面使用了旧版本,您可以从https://mvnrepository.com查看最新版本并更新 pom 脚本。

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>

If you use IntelliJ-IDEA, make sure your test file is in the Test Sources roots .如果您使用 IntelliJ-IDEA,请确保您的测试文件位于测试源根目录中
Because my project do not have the path src/test/java , when I use Ctrl + Shift + T to add a test file, it added in src/main/java ...因为我的项目没有路径src/test/java ,当我使用Ctrl + Shift + T添加测试文件时,它添加到src/main/java ...

See intellij support post请参阅Intellij 支持帖子

Maven马文

If using Maven, be sure to specify a dependency element inside the dependencies element.如果使用 Maven,请确保在dependency元素内指定一个dependencies元素。

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.4.0-RC1</version>
    <scope>test</scope>
</dependency>

If you want to use those Assertions outside of your test-related classes, in your regular app classes, drop the <scope>test</scope> element.如果您想在与测试相关的类之外使用这些断言,请在常规应用程序类中删除<scope>test</scope>元素。

Note that as of 5.4.0 of JUnit, we can specify the new single Maven artifact of junitjupiter which in turn will supply 8 libraries to your project.请注意,从 JUnit 5.4.0 开始,我们可以指定junitjupiter的新单个 Maven 工件, junitjupiter为您的项目提供 8 个库。 Very convenient if you are writing only JUnit 5 tests (Jupiter test engine), and not “vintage” JUnit 4 tests or other test engines.如果您只编写 JUnit 5 测试(Jupiter 测试引擎),而不是“老式”JUnit 4 测试或其他测试引擎,则非常方便。

If using Gradle rather than Maven, see the Answer by RileyManda .如果使用 Gradle 而不是 Maven,请参阅RileyManda答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM