使用Junit测试

一套测试就是一个强大的bug侦测器,能够大大的缩短查找bug的时间。

本次我们主要了解使用Junit对代码进行测试。

Junit中有以下这些方法可以对结果进行判定:

assertArrayEquals(expecteds, actuals) 查看两个数组是否相等。
assertEquals(expected, actual) 查看两个对象是否相等。类似于字符串比较使用的equals()方法
assertNotEquals(first, second) 查看两个对象是否不相等。
assertNull(object) 查看对象是否为空。
assertNotNull(object) 查看对象是否不为空。
assertSame(expected, actual) 查看两个对象的引用是否相等。类似于使用“==”比较两个对象
assertNotSame(unexpected, actual) 查看两个对象的引用是否不相等。类似于使用“!=”比较两个对象
assertTrue(condition) 查看运行结果是否为true。
assertFalse(condition) 查看运行结果是否为false。
assertThat(actual, matcher) 查看实际值是否满足指定的条件
fail() 让测试失败

现在我们来测试文件读取器FileReader

第一步:准备一个测试文件read,可以是任意格式的文件,内容如下:

Lily 1 90 80 72
Jack 2 88 91 80
Peter 3 69 93 70

第二步:创建一个测试类TestFileReader

package File;
import static org.junit.Assert.*;

import java.io.FileReader;
import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestFileReader {
}

第三步:添加setUp和tearDown方法

setup用于产生文件读取对象,teardown用于删除文件对象,因为Java中有自带的垃圾回收处理机制,所以不需要额外的删除对象,在这里我们使用teardown用来关闭文件。

public class TestFileReader {
    FileReader _input;
    
    @Before
    public void setUp() throws Exception {
        try {
            _input = new FileReader("E:/code/Rent/src/File/read");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("file can't open");
        }
    }

    @After
    public void tearDown() throws Exception {
        try {
            _input.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("file can't close");
        }
    }
}
setUp&&tearDown

第四步:添加测试代码

        @Test
    public void testRead() throws IOException {
        char ch='&';
        for(int i = 0; i < 3; i++){
            ch = (char) this._input.read();
        }
        assertEquals(ch,'l');
    }
    
    @Test
    public void testReadAtEnd() throws IOException{
        int ch = -111111;
        for(int i = 0; i < 53; i++){
            ch = this._input.read();
        }
        assertEquals(ch,-1);
    }    
Test Code

注意:我们这里使用的是junit4,不是junit3.junit4比junit3更加方便简洁。

这里我们就微微的介绍一下junit4和junit3的不同。以及junit4的用法

不同:

1)junit4最大的特点就是引入了注释,通过注释来执行代码。如:在setUp函数之上加before就是先执行,在tearDown上after就是后执行,在测试代码上加test就是表示是测试代码。不用再根据命名来执行。

2)junit4不需要继承testcase类

3)集成测试时,直接在类之上注释添加加入的单元测试

@RunWith(Suite.class)

@Suite.SuiteClasses({ TestCase1.class, TestCase2.class })

4)测试异常时,junit3是使用try-catch在异常之后添加fail。junit4添加@Test,使用参数“expected”,并指明抛出异常的Exception类:

//越界异常
@Test(expected = IndexOutOfBoundsException.class ) 
public void testCase2() {         
        /* test case 2*/          
        ArrayList emptyList = new ArrayList();    
        Object o = emptyList.get(0);    
}

5)设置超时,junit4使用"timeout"参数。如果测试运行的时间超过指定的毫秒数,则测试失败。

@Test(timeout=3000) 
public void remoteBaseRelativeResolutionWithDirectory()    
 throws IOException, ParsingException { 
  readBuilder.parse("config.xml" );    
}

6)运行测试类,junit4不需要添加main函数,直接运行junit即可

7)参数化测试,junit4中的测试类不再使用构造函数添加参数,而是只写一个测试函数,把这若干种情况作为参数传递进去,一次性的完成测试。在测试类的上面加@RunWith(Parameterized.class)

@RunWith(Parameterized.class)
public class JavaTest {
private int param;
private int param2;
private int result;

 @Parameters public static Collection data() {
return Arrays.asList(new Object[][]{ { 2, 4, 6 }, { 0, 0, 0 }, { -3, 9, 6 } });
}

 // 构造函数,对变量进行初始化
public JavaTest(int param, int param2, int result) {
this.param = param;
this.param2 = param2;
this.result = result;
}

@Test public void run() {
//do some thing use args, and assert it
int expected = param + param2;
assertEquals(expected, result);
}
} 
http://www.cnblogs.com/Natural-way/p/5204407.html
 
 
原文地址:https://www.cnblogs.com/sker/p/6755916.html