IDEA中快速使用单元测试—junit4举例


目录
  • 1. 新建一个测试类StringTest
  • 2. 在类中编写测试方法test1
  • 3. 方法上,加 [@Test] 注解
  • 4. 按 [Alt + Enter],导入 [junit4] 测试包
  • 5. 导入成功,测试结果

1. 新建一个测试类StringTest

public class StringTest {
    private int number;
    private String name;

    public StringTest() {
    }

2. 在类中编写测试方法test1

public class StringTest {
    private int number;
    private String name;

    public StringTest() {
    }

    public void test1() {
        System.out.println("hello, world!");
    }
}

3. 方法上,加 [@Test] 注解

public class StringTest {

    private int number;
    private String name;

    public StringTest() {
    }

    @Test
    public void test1() {
        System.out.println("hello, world!");
    }

}

4. 按 [Alt + Enter],导入 [junit4] 测试包

5. 导入成功,测试结果

导入成功后,[External Libraries] 中多了 [JUnit4]

点击 [test1] 方法,运行