Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit5: Trouble Importing Assertions

I'm trying to use JUnit5 to create some basic unit tests. I go to my Analyzer.java class and get the popup for creating a test. I hit Create New Test, setting Testing Library to 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:

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

Unfortunately, Assertions is red (this is in 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!

like image 792
anon_swe Avatar asked Jan 05 '17 19:01

anon_swe


3 Answers

Gradle

Add the following dependency to your 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")
like image 89
RileyManda Avatar answered Sep 30 '22 21:09

RileyManda


I don't know you are using or maven or gradle. But if you are using maven just add below dependecies in between your tags. 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/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>
like image 40
Isuru Dewasurendra Avatar answered Sep 30 '22 22:09

Isuru Dewasurendra


If you use IntelliJ-IDEA, make sure your test file is in the Test Sources roots.
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...

See intellij support post

like image 22
Jerry King Avatar answered Sep 30 '22 22:09

Jerry King