2

I want to add a test framework to my current maven project in IntelliJ.

I choose AsserJ, because the style looked best to me. I tried their tutorial, but it fails to provide enough help. (http://joel-costigliola.github.io/assertj/assertj-core-quick-start.html)

My issues is that the scope "test" is not explained well enough. With that scope I cant even import the necessary AsserJ files. When I try "import static org.assertj.core.api.Assertions.*;", the import is not recognized and it says that Assertions cant be resolved.

Any suggestions that might help?

Tigerware
  • 3,196
  • 2
  • 23
  • 39
  • 3
    what is the structure of your code? https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html might help. As "test" scope will only work for classes that are under the src/test/java file directory – Ash Oct 03 '16 at 12:41
  • Thank you. I had found that page before, but it didnt solve my confusion. Your post did help me though, even if it contained the same info that is on that page! Should I delete this question now, since it was maybe somewhat stupid? Or post your comment as the answer that helped? – Tigerware Oct 03 '16 at 13:37
  • Ill post as an answer, to try and claim all of that glorious reputation – Ash Oct 03 '16 at 13:41

2 Answers2

3

What is the structure of your code?

https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html might help.

"Test" scope imports libraries that are only available to classes that are under src/test/java

Ash
  • 2,562
  • 11
  • 31
3

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>
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • I believe the comment about "closed ticket #520" is meant to hyperlink to https://github.com/assertj/assertj/issues/520#issuecomment-156573016 – tarilabs Oct 11 '22 at 12:45