17

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?

LuCio
  • 5,055
  • 2
  • 18
  • 34

3 Answers3

17

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"

Till Brychcy
  • 2,876
  • 1
  • 18
  • 28
  • 1
    I've did an Maven "Update Project" on an other project having the same issue. It solved it. I think this is the appropriate answer regarding how to solve it.. – LuCio Jun 29 '18 at 13:15
  • 1
    We just updated to Photon, and all our src/test/** paths are marked with "Contains test sources=No" (using m2e 1.9.0.20180606-2036). What part of the pom.xml tells m2e what paths contain test sources? – Simon Jul 09 '18 at 11:57
  • From a quick look at the code: m2e asks maven for the values of https://maven.apache.org/ref/3.5.0/apidocs/org/apache/maven/project/MavenProject.html#getCompileSourceRoots() (or the test variation) , which are usually configured based on `` and `` but it only does that if executions of `maven-compiler-plugin` are configured and not set to be ignored by m2e – Till Brychcy Jul 09 '18 at 12:10
  • 1
    Thanks @till-brychcy, adding an empty 'maven-compiler-plugin' build configuration did the trick – Simon Jul 09 '18 at 12:28
  • Unfortunately m2e version 1.9 skips over and actually turns "Contains test sources" off again after Alt+F5 – Terran Jul 19 '18 at 16:41
  • Hmm in your comment in the other questions it sounded like you had downgraded m2e to 1.8. With 1.9 it should work. You do have `maven-compiler-plugin` configured and not set to be ignored by m2e? – Till Brychcy Jul 19 '18 at 16:46
12

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.

Xsasan
  • 400
  • 3
  • 14
LuCio
  • 5,055
  • 2
  • 18
  • 34
2

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.

Al W
  • 89
  • 3