Java 工具 JUnit单元测试

Java 工具 JUnit单元测试

@author ixenos

1.1.   JUnit单元测试框架的基本使用


一、搭建环境:

       导入junit.jar包(junit4)

二、写测试类:

       0,一般一个类对应一个测试类。

       1,测试类与被测试类最好是放到同一个包中(可以是不同的源文件夹)

       2,测试类的名字为被测试类的名字加Test后缀。

三:写测试方法:

       0,一般一个方法对应一个单元测试方法。

       1,测试方法的名字为test前缀加被测试方法的名字,如testAddPerson()。

       2,单元测试方法上面要加上@Test注解(org.junit.Test)!

       3,单元测试方法不能有参数,也不能有返回值(返回void)!测试的方法不能是静态的方法。

四、测试方法的基本使用:

       1,可以单独执行一个测试方法,也可以一次执行所有的、一个包的、一个类中所有的测试方法。

       2,执行完后,显示绿色表示测试成功;显示红色表示测试失败(抛异常后会测试失败)。

 1 package JUnitTest;
 2 
 3 import java.util.Arrays;
 4 
 5 import org.junit.Test;
 6 
 7 public class Demo1 {
 8     public static void main(String[] args) {
 9         System.out.println("HelloWorld");
10     }
11 
12     @Test
13     public void sort() {
14         int[] array = new int[] { 1, 2, 3, 4, 8, 5, 100, 34, 55 };
15         for (int i = 0; i < array.length - 1; i++) {
16             for (int j = i + 1; j < array.length; j++) {
17                 if (array[i] > array[j]) {
18                     int tmp = array[j];
19                     array[j] = array[i];
20                     array[i] = tmp;
21                 }
22             }
23         }
24         System.out.println(Arrays.toString(array));
25     }
26     
27     public static String sort1() {
28         int[] array = new int[] { 1, 2, 3, 4, 8, 5, 100, 34, 55 };
29         for (int i = 0; i < array.length - 1; i++) {
30             for (int j = i + 1; j < array.length; j++) {
31                 if (array[i] > array[j]) {
32                     int tmp = array[j];
33                     array[j] = array[i];
34                     array[i] = tmp;
35                 }
36             }
37         }
38         return Arrays.toString(array);
39     }
40     
41 }

1.2.   Assert断言工具类


其中有一些静态的工具方法(不符合期望就抛异常):

       assertTrue(...)        参数的值应是true

       assertFalse(...) 参数的值应是false 

       assertNull(...)         应是null值

       assertNotNull(...)   应是非null的值

      

       assertSame(...)              使用==比较的结果为true(表示同一个对象)

       AssertNotSame(...) 使用==比较的结果为false

       assertEquals(...)     两个对象equals()方法比较结果为true

 1 package JUnitTest;
 2 
 3 import org.junit.Test;
 4 
 5 import junit.framework.Assert;
 6 
 7 /**
 8  * 在以前判断方法正误:
 9  * 1.如果需要测试,都需要在main方法上调用,实际上每个类的main方法也可看成一种测试手段
10  * 2.结果都需要人工对比来判断程序正误
11  * 
12  * 有了JUnit后:
13  * 1.对方法的测试可模块化进行
14  * 2.在测试类中规定程序的正误判断
15  * 3.点击方法名测试一个方法,点击类名测试测试类中所有Test方法
16  * 
17  * 如果只是判断结果:
18  * 1.可以使用断言 Assert.assertSame(expected, actual);
19  * 2.断言是JUnit框架的部分
20  * 
21  * jUnit规范
22  * 1.一个类如果需要测试,那么就应该对应着一个测试类,测试类的命名规范:被测试类名+Test
23  * 2.一个被测试的方法一般对应着一个测试的方法,测试方法命名规范:test+被测试的方法名
24  * 
25  * 偷懒技巧:
26  * 1.直接在源代码的方法标记@Test,测试完记得删去
27  * 2.@Test方法不能标记static方法,也不能带有形参,可以另外写个小方法在旁边,
28  *     然后调用带有形参的方法
29  * 
30  *
31  */
32 
33 // 测试类
34 public class Demo2 {
35     
36     @Test
37     public void testSort() {
38         String ar = Demo1.sort1();
39         if (ar == "haha") {
40             throw new RuntimeException();
41         } else {
42             System.out.println("fuck");
43         }
44         Assert.assertTrue(ar != "haha");
45 
46     }
47 
48 }

1.3.   用于准备环境、清理环境的方法


@Test

       表示单元测试方法。

@Before

       所修饰的方法应是非static的(且没有参数,返回值为void)。

       表示这个方法会在本类中的每个单元测试方法之前都执行一次。

@After

       所修饰的方法应是非static的(且没有参数,返回值为void)。

       表示这个方法会在本类中的每个单元测试方法之后都执行一次。

@BeforeClass

       所修饰的方法应是static的(且没有参数,返回值为void)。

       表示这个方法会在本类中的所有单元测试方法之前执行,只执行一次。

@AfterClass

       所修饰的方法应是static的(且没有参数,返回值为void)。

       表示这个方法会在本类中的所有单元测试方法之后执行,只执行一次。

 1 package JUnitTest;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 
 6 import org.junit.After;
 7 import org.junit.Before;
 8 import org.junit.Test;
 9 
10 /**
11  * 当所测试的方法需要准备环境时,为了让测试方法更纯粹,将准备测试环境和删除测试环境分离在外面
12  * 在jUnit框架中,有@Before和@After两个注解用来分别处理
13  * 不过,当批量测试几个需要准备环境的方法时,程序会不断调用准备和删除,此时,可以通过-
14  * -@BeforeClass和@AfterClass注解来标记,但此时将准备和删除改为静态方法static
15  *
16  */
17 public class Demo3 {
18 
19     //准备测试的环境
20     @Before
21     //@BeforeClass
22     public void beforeRead(){
23         System.out.println("Before");
24     }
25     
26     //读取文件数据,吧文件
27     @Test
28     public void readFile() throws IOException{
29         FileInputStream fileInputStream = new FileInputStream("F:\a.txt");
30         int content = fileInputStream.read();
31         System.out.println("内容" + content);
32         fileInputStream.close();
33     }
34     
35     //第二个测试的方法
36     @Test
37     public void haha(){
38         
39     }
40     
41     //清理测试的环境
42     @After
43     //@AfterClass
44     public void afterRead(){
45         System.out.println("After");
46     }
47     
48 }
原文地址:https://www.cnblogs.com/ixenos/p/5777114.html