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!
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")
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With