1

I created a pom.xml that includes the XML to import JUnit 4. I opened up project in IntelliJ. It seems to have downloaded the external dependency, but when I try to use the dependency in an import statement, IntelliJ can't recognize it.

Here are the steps I took from the beginning:

  1. I created a pom.xml.
  2. mkdir MAVEN
  3. mv pom.xml MAVEN
  4. Open IntelliJ
  5. File > open > pom.xml
  6. cd ~/MAVEN
  7. mkdir -p src/main/java
  8. Go back to IntelliJ
  9. Right click on src -> mark directory as root

I believe that Maven imported everything correctly because I can see JUnit as a dependency in the project directory: https://imgur.com/TdJq7PK

Here is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.org</groupId>
    <artifactId>mvn-prac</atifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

When I attempt to use a common class from JUnit, such as import org.junit.Test;, IntelliJ can't recognize the symbol.

I have attempted both mvn clean and mvn install in the root of the project, and neither seemed to help.

Thanks for any help.

Logan Phillips
  • 660
  • 2
  • 10
  • 23
  • Did you set a proper JDK for your project in IntelliJ? – mr_m1m3 Feb 07 '19 at 10:51
  • Delete .idea directory and use Import Project instead. see https://stackoverflow.com/a/42427510/104891 if importing doesn't set roots correctly or doesn't configure the dependencies. It should be performed automatically for Maven projects. Your dependency has `test` scope, make sure the tests are in `src/test/java` so that they can see this dependency. – CrazyCoder Feb 07 '19 at 10:56
  • @CrazyCoder the entire problem was because of the test scope. I just deleted that line and it picks everything up. I was using the maven docs on apache's website and didn't even bother to care about what actually did. Thank you. – Logan Phillips Feb 07 '19 at 11:47

1 Answers1

1

If you are using the test scope, JUnit dependency will be available only for the tests located in src/test/java, but not to the classes in src/main/java.

Either remove the <scope>test</scope> line in pom.xml or move your tests to the proper Test Sources root.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904