The accepted Answer is correct, but lacks details.
Maven
Add your dependency
element for AssertJ in the dependencies
element of your POM.
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
Delete <scope>test</scope>
If you are trying to use the AssertJ assertions outside of test classes, in your regular app classes, then pay attention to the scope
element. 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>
Java 8 required
AssertJ 3 requires Java 8 or later.
Verify the version of Java used by your compiler. In Maven verify this pair of elements:
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>