Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 933
Fisher Coder Avatar asked Jan 08 '17 16:01

Fisher Coder


People also ask

How do I run a junit test in Maven project in IntelliJ?

Run a single testFrom the context menu, select Create 'name of the module/project and name of a goal'. In the dialog that opens, specify a working directory that contains test you want to run and in the Command line field, specify a phase (specified automatically) and the -Dtest=TestName test command. Click OK.

How does IntelliJ resolve dependency issue?

In the Dependencies tool window, in the search field, start typing the name of your dependency. In the list of results select the one you need and click Add. For more information on working with the Dependencies tool window, refer to Package Search. IntelliJ IDEA adds the dependency to your pom.


1 Answers

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).

like image 98
Marc Scheib Avatar answered Sep 18 '22 17:09

Marc Scheib