Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse - org.junit cannot be resolved, even though it's in the Maven dependencies

I've decided to write yet another question in this topic, as no solution from other posts worked for me.

My problem is that eclipse is unable to locate org.junit package at all, even though it is available in Maven dependencies and all necessary classes can be seen after expanding junit-4.12.jar.

Screenshot of the problem

Some technical info about the project and the environment:

  • Java 9 + eclipse Oxygen.2 (4.7.2)
  • Project is split into Jigsaw modules.

Here's what I've already tried:

  • Removing scope from both the project's pom.xml and the parent's pom.xml. Parent's POM looks like this:
<?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>my-group-id</groupId>
    <artifactId>my-artifact-id</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>my-name</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency>
    </dependencies>
    <modules>
        <module>module.1</module>
        <module>module.2</module>
        <module>module.3</module>
        <module>module.4</module>
        <module>module.5</module>
        <module>module.6</module>
        <module>module.7</module>
    </modules>
</project>

...and subproject's POM looks like this:

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>my-group-id</groupId>
        <artifactId>my-artifact-id</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>module.4</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-module-4-name</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

As seen on the screenshot, the class is in the correct folder (src/test/java), so it's not a scope problem.

  • Using Quick-fix "Add JUnit 4 library to the build path".
  • Removing all .marker files from eclipse workspace.
  • Closing and reopening all projects in eclipse (multiple times).
  • Restarting eclipse (multiple times).
  • Cleaning projects in eclipse (multiple times).
  • Maven > Update Project... launched from eclipse for all projects (multiple times).
  • Possible solution from this answer - I don't have sourceDirectory element defined in any of my pom.xml files.
  • Update 24.02.2018: Removing everything from .m2 repository and downloading dependencies again - didn't help.

I think it's also worth mentioning, that:

  • Running mvn test results in:

    Results :
    
    Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
    
  • Launching the test from eclipse results in java.lang.Exception: No runnable methods, which is expected, as @Test annotation isn't resolved.
  • Running mvn verify results in BUILD SUCCESS.
  • Same problem happens, when I try to import org.apache.logging.log4j.core.Logger, so it's not a JUnit problem.
like image 526
Bartłomiej Zieliński Avatar asked Dec 24 '22 09:12

Bartłomiej Zieliński


1 Answers

My workspace projects had the same problem identifying junit and other libraries after upgrading to Eclipse Photon. What worked for me was Maven > Update Project (you can select all open projects that has the issue). My projects were all in Java 8.

like image 187
Adarsh Shetty Avatar answered Jan 25 '23 23:01

Adarsh Shetty