LuCio
LuCio

Reputation: 5193

Eclipse Photon does not resolve imports in test sources

I have moved to Eclipse Photon with an existing workspace. I have some Maven projects in this workspace. All projects did not have any errors in Eclipse Oxygen. After opening my workspace in Eclipse Photon all test-classes which import org.mockito.Mockito, org.springframework.mock and org.springframework.test have errors. These imports cannot be resolved though Eclipse is aware of them as I can jump into the classes.

Why can Eclipse Photon not resolve these imports? And how can I fix this?

Upvotes: 17

Views: 9680

Answers (3)

Al W
Al W

Reputation: 99

For my Maven based project, I was able to resolve the problem by going into the .classpath file for the project and add a...

<attribute name="test" value="true"/>

tag to the attributes, i.e.,

<classpathentry kind="src" output="target/test-classes" path="src/test/java">
    <attributes>
        <attribute name="optional" value="true"/>
        <attribute name="maven.pomderived" value="true"/>
        <attribute name="test" value="true"/>
    </attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
    <attributes>
        <attribute name="maven.pomderived" value="true"/>
        <attribute name="test" value="true"/>
    </attributes>
</classpathentry>

Hope that helps.

Upvotes: 2

LuCio
LuCio

Reputation: 5193

I solved it and want to share my results.

The build path properties in eclipse photon have a new option in the source tab: Contains test sources. This option was set to No for the folder myproject/src/test/java. When I tried to set it to Yes I got the following error:

The source folder 'src/testjava' in project 'myproject' must have an output folder that is not also used for main sources

The error was shown although the output folder was already set to a different path than that of myproject/src/main/java. To reset the settings I unchecked the option Allow outoput folders for source folders and set all output folders again. After that I was able to set the mentioned option to Yes. As the result of it the imports got resolved.

The reason is described here (scroll down there to Test sources). It says:

For each project, compilation is now done in two phases: First all main sources (which cannot see any test-code on the build-path) and then all test sources.

Since the option wasn't set for the test-source-folder it was compiled like a main-source-folder. Therefore the imports of test classes from dependencies with scope test could not be resolved.

Upvotes: 12

Till Brychcy
Till Brychcy

Reputation: 2924

If this is really a maven project and you are using matching m2e-version 1.9, it should automatically configure the "Contains test sources."-setting and the related settings correctly.

You may have to update the project classpath by right-clicking on the project and Choosing "Maven" > "Update Project"

Upvotes: 17

Related Questions