intellij-idea

IntelliJ cannot resolve Junit for Maven project


The closest post I could find on SO is: Why am I getting "Cannot resolve symbol"?

But it didn't help me. :(

Here's my error screenshot: commercial photography locations

And I've made sure that my pom.xml is scoped to and my actual code is also put under test folder. Here's my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.fishercoder</groupId>
    <artifactId>leetcode-algorithms</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

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

Also

mvn clean compile

runs success.

Here's the result of

mvn test

Here:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.stevesun.AddBinaryTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.042 sec
Running com.stevesun.MissingRangesTest
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

Tests run: 10, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.913 s
[INFO] Finished at: 2017-01-08T09:13:22-08:00
[INFO] Final Memory: 16M/308M
[INFO] ------------------------------------------------------------------------

I'm guessing it's something funky with my IntelliJ setup?


Solution

  • EDIT - Solved via Comments: In your screenshot, there is a test.iml located in your test folder. Is there perhaps an exclusion or something? Can you try deleting that file?


    You can try to Invalidate Caches in IntelliJ (via File | Invalidate Cache).

    You can also try and rebuild your project from within the IDE (via Build | Rebuild Project).

    You could also try to Synchronize the Project (Reimport All Maven Projects) via Maven (there should be a Maven Tab somewhere on the right side or via typing Maven Projects into Ctrl + Shift + A).