Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 337
LuCio Avatar asked Jun 29 '18 11:06

LuCio


People also ask

How do I fix all imports in eclipse?

1. Go to the line of unused import, press Ctrl + 1, which is an Eclipse shortcut of a quick fix. This will show a drop-down menu to fix this error and one of them will be “remove unused imports.” It will remove that import statement from the Java file.


2 Answers

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"

like image 70
Till Brychcy Avatar answered Oct 16 '22 16:10

Till Brychcy


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.

like image 35
LuCio Avatar answered Oct 16 '22 17:10

LuCio