利用Junit对Java项目做简单的单元测试

参考:http://tonl.iteye.com/blog/1948869


Junit的环境配置
环境:Eclipse 中配置junit,选择项目Junit4(在Package Explorer中) -> 右击鼠标 -> 选择properties -> 选择Java Build Path (在左边的菜单中) -> 在右边选择标签 Libraries  -> 单击按钮“Add Library”  -> 选择JUnit , 单击按钮 “Next>”  -> 选择JUnit library version 为: JUnit4 -> 单击按钮“Finish” –> 单击按钮 “OK”

1. 首先写自己想测的代码;

<pre name="code" class="java">package com.baidu.recsys.myapp.model;


public class Calculator { 
	
	public int add(int a, int b){
		return a + b;
	}
	
	public int minus(int a, int b){
		return a - b;
	}
	
	public int multiply(int a, int b){
		return a * b;
	}
	
	public int divide(int a, int b) throws Exception{
		if(0 == b){
			throw new Exception("cannot be zero.");
		}
		return a / b;
	}

	public void squareRoot(int i) {
		// TODO Auto-generated method stub
		//死循环 bug
		for(;;){
			;
		}
	}

}

 2.利用Junit测试; 

package com.recsys.myapp.test;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import com.baidu.recsys.myapp.model.Calculator;

public class CalculatorTest {

	private Calculator cal = new Calculator();

	@BeforeClass // 注意,这里必须是static...因为方法将在类被装载的时候就被调用(那时候还没创建实例)
	public static void before()
	{
	    System.out.println("global");
	}

	@AfterClass
	public static void after() {
		System.out.println("global destroy");
	}

	@Before
	public void setUp() throws Exception {
		System.out.println("一个测试开始");
	}

	@After
	public void tearDown() throws Exception {
		System.out.println("一个测试结束");
	}

	@Test
	@Ignore
	public void testAdd() {
		int result = cal.add(1, 2);
		Assert.assertEquals(3, result);
	}

	@Test
	public void testMinus() {
		int result = cal.minus(5, 2);
		Assert.assertEquals(3, result);
	}

	@Test
	public void testMultiply() {
		int result = cal.multiply(4, 2);
		Assert.assertEquals(8, result);
	}

	@Test(timeout = 1000) // 单位为毫秒
	public void testSquareRoot() {
		cal.squareRoot(4);
	}

	@Test(expected = Exception.class)
	public void testDivide() throws Exception {
		cal.divide(4, 0);// 很简单的办法.......
	}

//	@Test
//	public void testDivide() {
//		int result = 0;
//		try {
//			result = cal.divide(10, 5);
//		} catch (Exception e) {
//			e.printStackTrace();
//			Assert.fail();// 如果这行没有执行。说明这部分正确。
//		}
//		Assert.assertEquals(2, result);
//	}
}


3. 运行程序

右键项目→run as → Junit Test


4. 验证结果



结果解释:

5个case,add测试用例ignore,通过3个case,testSquareRoot因为超时不通过;结果报错;


5. before after setUp testDown解释


@BeforeClass @AfterClass在类装载和类卸载时调用;

@Before @After在测试case每次执行前后都会被调用;


顺便一提,Java类的生命周期:

类的生命周期

  加载 loading

  验证 verification

  准备 preparation

  解析 resolution

  初始化 initialization

  使用 using

  卸载 unloading


PS:在test方法内除了使用Assert的assertEquals()方法外,还能使用assertFalse()、assertTrue()、assertNull()、assertNotNull()、assertSame()、assertNotSame()等断言函数。而且如果使用的是Junit4,结合Hamcrest,使用

assertThat([value], [matcher statement])方法可以实现更灵活的断言判断(前提是引入hamcrest的jar包)。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值