Satyam Priyam
Satyam Priyam

Reputation: 21

JUnit on IntelliJ not working

I tried setting up JUnit 5 on my INtelliJ IDEA Community Edition 2018.2. The jar was downloaded but I am getting Cannot resolve symbol Assertions on importing

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

Error

Upvotes: 2

Views: 1923

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 338564

Are you trying to use the JUnit assertions in a regular app class rather than a test class?

Delete <scope>test</scope>

When a Maven dependency carries a scope element with a value of test, that means you cannot use that library outside of your test-specific source package/folder.

If you are trying to call JUnit from code in your example project’s src/main/java/… folder hierarchy, you will see that error. If you call JUnit from src/test/java…, you will see success.

To enable JUnit in the src/main/java/… folder hierarchy, delete the scope element in your POM dependency. So this:

<!-- 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>

…becomes this:

<!-- 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>
</dependency>

By the way, note that as of 5.4.0 of JUnit, we can specify the new and very convenient single Maven artifact of junit-jupiter which in turn will supply 8 libraries to your project.

Upvotes: 3

Related Questions