javamavenjunitjupyter

junit import not working in IntelliJ IDEA


I have this import in intelliJ IDEA:

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

and it says

cannot resolve symbol 'api'

package com.programming.techie;

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

class ContactManagerTest {

}

I have these dependencies in maven:

     <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.13.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.10.0</version> <!-- Or the latest stable version -->
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.10.0</version> <!-- Or the latest stable version -->
        <scope>test</scope>
    </dependency>

Solution

  • To use

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

    You only need to have the following in pom.xml

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

    Avoid using different versions of the junit-jupiter library.

    You can do one of the two things.

    1. Remove the other junit dependencies (or)
    2. Make all the junit dependencies the same version in pom.xml

    In case your error is not solved after doing this, try doing "Invalidate cache and restart" in IntelliJ and then do a maven reload.

    After that, it should work fine.