Junit 简介
JUnit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个。 JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具。
JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。
Junit 3.x 安装使用
1.从Junit.org 下载Junit 3.x 版本,在第二步中称为 junitx.x.x.zip.
2.把junitx.x.x.zip 解压到任何目录。例如:D:\tools\junit。
安装完毕。
打开一个命令窗口到D:\tools\junit。
可以用如下命令run 一下junit 自带的例子:
java -cp junit.jar;. junit.swingui.TestRunner junit.samples.AllTests
将会看到Juint 自带的窗口UI(如下图)。
第一个Junit单元测试
待测类:
public class Largest{
public static int largest(int[] list){
int index, max = Integer.MAX_VALUE;
for(index=0; index < list.length-1; index++){
if(list[index] > max){
max = list[index];
}
}
return max;
}
}
测试类:
import junit.framework.*;
public class TestLargest extends TestCase {
public TestLargest(String name){
super(name);
}
public void testSimple(){
assertEquals(9, Largest.largest(new int[]{9,8,7}));
}
}
打开命令窗口到源文件目录,用如下命令编译java 源文件:
public class Largest{
public static int largest(int[] list){
int index, max = 0;
for(index=0; index < list.length-1; index++){
if(list[index] > max){
max = list[index];
}
}
return max;
}
}
Passed. Ju bar is green.
public class TestLargest extends TestCase {
public TestLargest(String name){
super(name);
}
public void testSimple(){
assertEquals(9, Largest.largest(new int[]{9,8,7}));
}
public void testSimple1(){
assertEquals(9, Largest.largest(new int[]{8,9,7}));
}
public void testSimple2(){
assertEquals(9, Largest.largest(new int[]{7,8,9}));
}
}
Oh no no,又有bug了。检查待测代码.... 修改源代码....编译.....测试.
public class Largest{
public static int largest(int[] list){
int index, max = 0;
for(index=0; index <= list.length-1; index++){
if(list[index] > max){
max = list[index];
}
}
return max;
}
}
Green. Once again!

import junit.framework.*;
public class TestLargest extends TestCase {
public TestLargest(String name){
super(name);
}
public void testSimple(){
assertEquals(9, Largest.largest(new int[]{9,8,7}));
}
public void testSimple1(){
assertEquals(9, Largest.largest(new int[]{8,9,7}));
}
public void testSimple2(){
assertEquals(9, Largest.largest(new int[]{7,8,9}));
}
public void testSimple3(){
assertEquals(9, Largest.largest(new int[]{7,9,8,9}));
}
public void testSimple4(){
assertEquals(1, Largest.largest(new int[]{1}));
}
public void testSimple5(){
assertEquals(-7, Largest.largest(new int[]{-7,-8,-9}));
}
}
My god!!!

public class Largest{
public static int largest(int[] list){
int index, max = Integer.MIN_VALUE;
for(index=0; index <= list.length-1; index++){
if(list[index] > max){
max = list[index];
}
}
return max;
}
}
OK,心力交瘁,但还是搞定。
public class Largest{
public static int largest(int[] list){
int index, max = Integer.MIN_VALUE;
if(0 == list.length){
throw new RuntimeException(" Empty list!");
}
for(index=0; index <= list.length-1; index++){
if(list[index] > max){
max = list[index];
}
}
return max;
}
}
加一个测试空数组的用例:
public void testEmpty(){
try{
Largest.largest(new int[]{});
fail("It should be throw exception!");
}catch (RuntimeException e){
assertTrue(true);
}
}
就这样,莫名其妙的pass了......

import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class LargestTest {
private int expected;
private int[] input;
public LargestTest(int exp, int[] inp){
this.expected = exp;
this.input = inp;
}
@Parameters
public static Collection preparData(){
Object[][] object = {{9, new int[]{9,8,7}},{9, new int[]{8,9,7}},{8, new int[]{7,8,9}},{9, new int[]{7,9,8,9}},{1, new int[]{1}},{-7, new int[]{-7,-8,-9}}};
return Arrays.asList(object);
}
@Test
public void testLargest() {
assertEquals(expected, Largest.largest(input));
}
}