2

My IntelliJ IDEA sees the JUnit 4 Maven dependencies.

Dependecies of JUNIT in the project

But does not resolve any symbol coming from these dependencies

enter image description here

Any idea?

Ravi Parekh
  • 5,253
  • 9
  • 46
  • 58
Michael Courcy
  • 627
  • 2
  • 6
  • 13
  • 1
    Did you try clean/rebuilding your project as a sanity check question :)? – Shiraaz.M Nov 29 '15 at 20:29
  • I didn't find any clean action, but i use the build > Rebuild Project and it says package org.junit.runner does not exist and 4 other errors of the same kind. – Michael Courcy Nov 29 '15 at 20:39
  • This is a similar question to yours http://stackoverflow.com/questions/23579746/using-intellij-maven-to-import-libraries-cannot-resolve-symbol?rq=1 – Shiraaz.M Nov 29 '15 at 20:43
  • Yes and I also did both : reimport and use the same maven installation. But with no effect. – Michael Courcy Nov 29 '15 at 20:53
  • 1
    `junit` in `test` scope and `BookSearchTest` test class under source directory (`src/main/java`) ? – Ori Dar Nov 29 '15 at 21:02
  • I finally build the project with eclipse and reimport in Inteliji. No class path problem anymore. So it's solved, I guess i missed something during the creation of the project. Thanks for your help. – Michael Courcy Nov 29 '15 at 23:27

3 Answers3

1

I followed these steps and was able to code a TestCase with Intellij code insight.

In a shell, I did the following

mkdir so33987661
mkdir so33987661/src/
mkdir so33987661/src/test
mkdir so33987661/src/test/java
cd so33987661
vi pom.xml

My pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" 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>org.so33987661</groupId>
    <artifactId>my-project</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Back to Intellij, choose File menu, New, Project from Existing Sources.

Open the so33987661/pom.xml file.

Check the Import Maven Projects Automatically. enter image description here

Accept the defaults for the rest of the screens by clicking Next until the project is created.

Navigate to the src/test/java folder and create a new test and code, code, code.

import org.junit.Test;
import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class BookSearchTest {

    @Test
    public void testIt() throws Exception {
        // todo: testIt()!
    }
}

enter image description here

Kirby
  • 15,127
  • 10
  • 89
  • 104
  • I delete the .idea file and reimport the project, the problem was fixed then. That was my firts try of intellij I guessed I mess something. I can't remember if "Import Maven Projects Automatically" was checked when this problem happened, but it is now.Thanks anyway. – Michael Courcy Jan 02 '16 at 11:33
  • For me, this solution worked even without reimporting project (just unchecking and checking back Import automatically.. choice). – Martin D Oct 11 '17 at 19:21
0

I encountered the same problem. Open your pom.xml, find "junit". See scope value, maybe 'test'. if u import junit in main package, intellij idea will throw error, but not eclipse. Use junit in right scope can solve the problem. I fixed my error.

  • That was not the case, the test was in the standard directory, the project was a classical maven project. – Michael Courcy Jan 02 '16 at 11:23
  • I'm not sure my scope was test but if it was a standard scope it should be seen in a test. I delete the .idea and reimport the project without changing anything else and the problem solved out. Thanks anyway. – Michael Courcy Jan 02 '16 at 11:27
0

# 1 check in settings -> build, execution, deployment -> build tools -> maven, if "User settings file" and (if you are using local repo) "Local repository" paths are set correctly.

# 2 check in settings -> build, execution, deployment -> compiler -> java compiler if target bytecode version is that you expecting. (sometimes after adding new dependency Intelli is setting it to 1.5)

# 3 in project settings (default shortcut ctrl+alt+shift+s) project settings -> project

  • ProjectSDK should point to correct destination

  • Project language level should be version that you are using

in project settings -> modules Language Level should be version that you are using

over9k
  • 115
  • 1
  • 10
  • Thanks for this answers, that will be useful later on but as I said in my previous comment I rebuilt the project and the problem solved out. – Michael Courcy Jan 02 '16 at 11:21