简体   繁体   中英

Cannot resolve symbol 'Assertions' <— Error message when trying to use AssertJ in IntelliJ

Similar to some other Questions, I find IntelliJ mysteriously refuses to recognize AssertJ library. I am asking again as (a) I have tried the various suggestions, and (b) I have a very simple example anyone can try themselves.

In IntelliJ 2018 and IntelliJ 2019 pre-release, I create a new project using the Maven archetype maven-archetype-quickstart version 1.4.

AssertJ 3 requires Java 8 . So I changed these two lines in the POM for 1.7 to 11 .

    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>

I add this to the POM:

    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.11.1</version>
        <scope>test</scope>
    </dependency>

Using the Maven panel in IntelliJ, I executed a clean and install .

Seems good. I verify the org.assertj:assertj-core:3.11.1 library appears in the Project panel of IntelliJ. The app runs, with Hello World appearing on the console in IntelliJ.

In the App.java file, I add this import statement.

import static org.assertj.core.api.Assertions.* ; 

Error reported in the IDE editor:

Cannot resolve symbol 'Assertions'

Some people suggest a corrupted Maven cache. So I quit IntelliJ, and I delete the .m2 folder in my home folder. I re-open my project in IntelliJ, and re-execute the Maven clean & install . Many things are downloading, so I know the Maven cache is indeed being recreated.

Yet, still the error in my editor, Cannot resolve symbol 'Assertions' .

No Java Modules involved, as the quickstart archetype has not yet been updated for that.

Delete <scope>test</scope>

This topic was addressed in a closed ticket # 520 on the AssertJ issue tracker.

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 AssertJ from code in your example project's src/main/java/… folder hierarchy, you will see that error. If you call AssertJ from src/test/java… , you will see success.

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

    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.11.1</version>
        <scope>test</scope>
    </dependency>

…becomes this:

    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.11.1</version>
    </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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