Intellij Create Test缺少JUnit

时间:2019-03-27 21:24:48

标签: intellij-idea

我尝试创建一个测试类并使用JUnit对其进行测试,但是在我的Intellij中缺少JUnit库。

JUnit在库选项中需要做什么?

1 个答案:

答案 0 :(得分:3)

在Maven中添加junit-jupiter工件

如果使用Apache Maven来配置项目,并使用JUnit 5 Jupiter来编写测试,请向项目中添加一个依赖项。

JUnit 5.4通过提供此新的Maven工件junit-jupiter简化了事情。这一合计的工件提供了编写和运行JUnit 5测试所需的全部。在5.4之前的版本中,您必须添加多个工件-令人困惑和混乱。

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.5.0-M1</version>
    <scope>test</scope>
</dependency>

这里是一个示例,即整个POM文件。我使用maven-archetype-quickstart工件启动了一个应用程序。接下来,我将所有版本号更改为最新版本,如本周所示。最后,我用新的JUnit 5依赖关系替换了旧的JUnit 4依赖关系。

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>work.basil.example</groupId>
    <artifactId>method-lister</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>method-lister</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>12</maven.compiler.source>
        <maven.compiler.target>12</maven.compiler.target>
    </properties>

    <dependencies>
        <!--<dependency>-->
        <!--  <groupId>junit</groupId>-->
        <!--  <artifactId>junit</artifactId>-->
        <!--  <version>4.11</version>-->
        <!--  <scope>test</scope>-->
        <!--</dependency>-->

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.5.0-M1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M3</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.1.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>3.0.0-M1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>3.0.0-M1</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

JUnit插件

确保已安装 JUnit的IntelliJ插件。确认选中标记已选中,表示已启用。 (您可以禁用不需要的插件以节省内存和启动时间。)

screenshot of IntelliJ > Preferences > Plugins, filtered to show only JUnit-related plugins

Generate Test对话框

您的Generate> Test…的IntelliJ 2019.1对话框应如下所示。

screenshot of IntelliJ > Generate > Test… dialog box