简体   繁体   English

如何在JDK 9 for IntelliJ中向模块添加测试依赖项?

[英]How do I add test dependencies to a module in JDK 9 for IntelliJ?

I am building a project with maven 3.3.9 using java 9 (build 9-ea+165). 我正在使用Java 9(Build 9-ea + 165)使用Maven 3.3.9构建项目。 Tests ran and build were successful. 测试运行和构建成功。

However. 然而。 IntelliJ Idea 2017.1.2 is complaining and will not compile/run tests with the message 'The module does not have the module 'junit.jupiter.api' in its requirements. IntelliJ Idea 2017.1.2抱怨,并且不会编译/运行测试,消息为“模块的要求中没有模块'junit.jupiter.api”。

How do I add this for IntelliJ? 如何为IntelliJ添加它? Is it necessary? 有必要吗?

Screenshot: 截图: IntelliJ中编辑器的屏幕截图。

Project structure: 项目结构: 项目结构

From pom: 从pom:

<dependencies>
    <!-- testing -->
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0-M4</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                        <version>4.12.0-M4</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
        </plugin>
    </plugins>
</build>

It's possible that your project structure isn't valid anymore. 您的项目结构可能不再有效。 You can try to check if your folder containing your tests is your actual test folder: 您可以尝试检查包含测试的文件夹是否为实际的测试文件夹:

  1. Press [Ctrl] [Alt] [Shift] [S] to open Project Structure. 按[Ctrl] [Alt] [Shift] [S]打开项目结构。
  2. Open "Modules" 打开“模块”
  3. Open the "Sources tab" 打开“来源”标签
  4. Your folder containing the tests should be marked as "Tests" (the icon will be green) 您包含测试的文件夹应标记为“ Tests”(图标为绿色)

There are a few options 有几种选择

First, you may not have imported the project correctly into IntelliJ in the first place. 首先,您可能首先没有将项目正确导入到IntelliJ中。 In which case the quickest thing to do is often to simply close the project, remove the intellij files from the source directory ( .idea directories and any *.i?l files), and reopen the appropriate root pom file, using the open option (if it gives you the option, choose to create a new project and delete the old project). 在这种情况下,最快的操作通常是简单地关闭项目,从源目录中删除intellij文件( .idea目录和任何*.i?l文件),然后使用open选项重新打开相应的root pom文件(如果您可以选择,请选择创建一个新项目并删除旧项目)。

Second option is that you have correctly opened the project but you've not reimported it since changing the pom and you don't have autoimport switched on and you ignored the notifications asking you to reload. 第二种选择是您已经正确打开了项目,但是自从更改pom以来没有重新导入它,并且没有打开自动导入功能,并且忽略了要求您重新加载的通知。 In which case just open the maven window (usually from a horizontal button on the right of the screen) and hit the reimport button at the top left of the maven window. 在这种情况下,只需打开Maven窗口(通常从屏幕右侧的水平按钮),然后单击Maven窗口左上方的重新导入按钮即可。

The third option is to blow away your caches. 第三种选择是删除缓存。 This is easily done from the file menu. 从文件菜单可以轻松完成此操作。

There may be others. 可能还有其他。 I'd start from the bottom up. 我将从头开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM