Junit 入门(一)

    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 源文件:
        javac -cp "..\..\..\tools\junit\junit3.8.1\junit3.8.1\junit.jar" *.java

        编译完成之后, 用如下命令运行测试用例:
        java -cp .;"..\..\..\tools\junit\junit3.8.1\junit3.8.1\junit.jar" junit.swingui.TestRunner TestLargest

        Oh ho ho, 出师不利啊...
        


       Junit 报告测试failed,检查代码,发现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;
	}
}
        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!  微笑

        应该没有问题了吧???写出没有bug的代码也许比中500万都难...还是多加几个测试看看
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!!!惊讶 这回是脸都绿了...

    
       继续debug...看来是没考虑负数...修改源代码...编译...测试。
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,心力交瘁,但还是搞定。


        到此为止,看起来largest可以完美的工作了,但真的完美么???
        如果有人传入一个空数组的话,largest 的行为又是什么呢?
        是的,应该给它指定一个行为——抛出异常。
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了...... 疑问


        重新审视各个testSimpleX,一种“不高级”的感觉油然而生,原本这些test 只是测试的值不同而已,那我们能不能将这些值作为参数传递给测试用例呢???
        答案肯定是可以的.               (世界留给人们的想象空间真的很大???)  

         参数化测试实现:
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));  
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JUnit 是一个用于 Java 编程语言的单元测试框架。使用 JUnit 可以帮助开发人员编写更好的代码,并且保证代码的质量和可靠性。下面是 JUnit 入门使用教程: 1. 下载 JUnit 可以从 JUnit 官网下载最新版本的 JUnit,也可以使用 Maven 或 Gradle 等构建工具自动下载。下载后,将 JUnit 的 jar 文件添加到项目的 classpath 中。 2. 创建测试类 在项目中创建一个测试类,这个测试类需要继承 JUnit 提供的 TestCase 类或是使用注解方式来编写测试代码。下面是一个使用注解方式的测试类示例: ``` import org.junit.Test; import static org.junit.Assert.assertEquals; public class MyTest { @Test public void testAdd() { int result = 1 + 2; assertEquals(3, result); } } ``` 在这个例子中,使用了 `@Test` 注解来标记测试方法。`assertEquals()` 方法用来断言实际结果与预期结果是否一致。 3. 运行测试 使用 IDE 或命令行工具运行测试。在 IDE 中,可以右键点击测试类或测试方法,选择 Run As -> JUnit Test 运行测试。在命令行中,可以使用 Maven 命令 `mvn test` 运行测试。 4. 查看测试结果 测试运行后,JUnit 会生成测试报告,告诉你测试的结果和测试覆盖率等信息。可以在 IDE 中查看测试结果报告,也可以在命令行中查看 Maven 的测试报告。 上面就是 JUnit 入门使用教程的基本步骤,通过 JUnit 编写单元测试可以提高代码质量和可靠性,同时也方便开发人员进行代码重构和维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值