java 如何在intellij IDEA中向Java项目添加单元测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36877550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:58:18  来源:igfitidea点击:

How to add unit tests to Java project in intellij IDEA?

javaunit-testingintellij-ideajunit

提问by Gujarat Santana

I want to create simple java project with JUnit, so for example I'm want to write an algorithm like merge sort or some Java class and create test class so I can make unit test for that class.

我想用 JUnit 创建简单的 Java 项目,例如,我想编写一个算法,如合并排序或一些 Java 类并创建测试类,以便我可以为该类进行单元测试。

I've create the project with:

我已经创建了项目:

File -> New -> Project -> java -> next and setup the project name and location

File -> New -> Project -> java -> next 并设置项目名称和位置

and I want to make the unit test for the class the I've created, and I've tried the following solotions :

我想对我创建的类进行单元测试,并且我尝试了以下 solotions:

  1. solution 1 from IntelliJ IDEA doscusing the light bulb to create the test class
  2. solution 2using shortcut [ctrl + shift + t]
  1. 来自 IntelliJ IDEA dosc 的解决方案 1使用灯泡创建测试类
  2. 使用快捷方式的解决方案2[ctrl + shift + t]

But I always endup with import static org.junit.Assert.*;cannot resolve symbol 'junit', I tried different unit test library end up the same way.

但我总是以import static org.junit.Assert.*;无法解析符号 'junit'告终,我尝试了不同的单元测试库以同样的方式结束。

How to resolve this problem so I can make unit test class in this simple Java project?

如何解决这个问题,以便我可以在这个简单的 Java 项目中制作单元测试类?

采纳答案by duffymo

You can use Gradle or Maven (my personal preference these days).

您可以使用 Gradle 或 Maven(这些天我个人的偏好)。

But the easiest way is to add the JUnit JAR to your project, write some tests, and execute them in IntelliJ.

但最简单的方法是将 JUnit JAR 添加到您的项目中,编写一些测试,然后在 IntelliJ 中执行它们。

  1. Go to JUnitand download version 4.12 of the JAR. Put it in a folder /test-lib in your IntelliJ project.
  2. Create a folder /src and add a package /model and a Java class Foo to it (I'll write you one).
  3. Mark /src as a source root.
  4. Create a folder /test and add a package /model and a Java class FooTest to it (I'll write that, too).
  5. Mark /test as a test source root.
  6. Right click on /test and tell IntelliJ to "Run All Tests".
  7. IntelliJ will run all the tests and present the results in the Run window.
  1. 转到JUnit并下载 JAR 的 4.12 版。将它放在您的 IntelliJ 项目中的 /test-lib 文件夹中。
  2. 创建一个文件夹 /src 并向其中添加一个包 /model 和一个 Java 类 Foo(我会给你写一个)。
  3. 将 /src 标记为源根。
  4. 创建一个文件夹 /test 并向其中添加一个包 /model 和一个 Java 类 FooTest(我也会写)。
  5. 将 /test 标记为测试源根。
  6. 右键单击 /test 并告诉 IntelliJ“运行所有测试”。
  7. IntelliJ 将运行所有测试并在“运行”窗口中显示结果。

Here's the model class:

这是模型类:

package model;

public class Foo {
    private String value;

    public Foo(String v) { this.value = v; }

    public String toString() { return this.value; }
}

Here's the test class:

这是测试类:

package model;

public class FooTest {

    @Test
    public void testToString() {
        String expected = "Test";
        Foo foo = new Foo(expected);
        Assert.assertEquals(expected, foo.toString());    
    }          
}

回答by Gujarat Santana

I'm not sure this is the best solutions but I manage to build the unit test use gradle and maven. like this :

我不确定这是最好的解决方案,但我设法使用 gradle 和 maven 构建单元测试。像这样 :

create Java project :

创建 Java 项目:

File -> New -> Project -> Gradle -> choose only java-> fill the groupId and ArtifactId-> choose use default gradle wrapper-> enter project name and location ->finish

文件 -> 新建 -> 项目 -> Gradle -> 只选择 java -> 填写 groupId 和 ArtifactId -> 选择use default gradle wrapper-> 输入项目名称和位置 -> 完成

and from the root of the project

并从项目的根

right click -> Add Framework Support -> choose maven.

右键单击 -> 添加框架支持 -> 选择 maven。

from there I can create the class that I want and make the unit test using the solutions from the question [ctrl + shift +t] .

从那里我可以创建我想要的类并使用问题 [ctrl + shift +t] 中的解决方案进行单元测试。