Eclipse Photon 不解析测试源中的导入

Eclipse Photon does not resolve imports in test sources

我已将现有工作区移至 Eclipse Photon。我在这个工作区中有一些 Maven 项目。所有项目在 Eclipse Oxygen 中都没有任何错误。在 Eclipse Photon 中打开我的工作区后,所有导入 org.mockito.Mockitoorg.springframework.mockorg.springframework.test 的 test-类 都有错误。这些导入无法解析,尽管 Eclipse 知道它们,因为我可以跳转到 类.

为什么 Eclipse Photon 无法解析这些导入?我该如何解决这个问题?

我解决了,想分享我的结果。

eclipse photon 中的构建路径属性在源选项卡中有一个新选项:包含测试源。对于文件夹 myproject/src/test/java,此选项已设置为 。当我尝试将其设置为 Yes 时,出现以下错误:

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

尽管输出文件夹已设置为与 myproject/src/main/java 不同的路径,但仍显示错误。要重置设置,我取消选中选项 Allow outoput folders for source folders 并再次设置所有输出文件夹。之后,我能够将提到的选项设置为 Yes。结果,进口问题得到解决。

原因已描述 here(向下滚动到 测试来源)。它说:

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.

由于没有为测试源文件夹设置选项,因此它像主源文件夹一样被编译。因此,无法解析从范围为 test 的依赖项导入 test 类。

如果这真的是一个 maven 项目,并且您使用的是匹配的 m2e-version 1.9,它应该会自动正确配置 "Contains test sources."-setting 和相关设置。

您可能需要通过右键单击项目并选择 "Maven" > "Update Project"

来更新项目类路径

对于我基于 Maven 的项目,我能够通过进入项目的 .classpath 文件并添加一个...来解决问题...

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

标记属性,即

<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>

希望对您有所帮助。