Junit测试备忘录

最简单的Junit4x sample,个人认为作为程序员有时候看代码要比文字容易多了,当然代码必须注释规范、明了、易懂

/**
* Class Arithmetic
* 测试目标类
* @author lgsun
* Date: 2011-3-27
*/
package com.lgsun.target;

public class Arithmetic
{
private int parmaerOne;
private int parmaerTwo;

public int getParmaerOne()
{
return parmaerOne;
}
public void setParmaerOne(int parmaerOne)
{
this.parmaerOne = parmaerOne;
}

public int getParmaerTwo()
{
return parmaerTwo;
}
public void setParmaerTwo(int parmaerTwo)
{
this.parmaerTwo = parmaerTwo;
}

public int getSum()
{
return (parmaerOne+parmaerTwo);
}
}



/**
* Class ArithmeticTest
* Junit测试sample
* @author lgsun
* Date: 2011-3-27
*/
package com.lgsun.test;

import java.lang.reflect.Field;
import java.util.Random;

import junit.framework.Assert;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import com.lgsun.target.Arithmetic;

@SuppressWarnings("all")
public class ArithmeticTest
{
private static Logger logger = Logger.getLogger(ArithmeticTest.class.getName());
private Arithmetic arithmetic;

/**
* 只有在类运行前执行一次
* 一般用于做类级别的初始化
* 可以加入耗费资源较大而且不必每次还原的初始化部分
*/
@BeforeClass
public static void setUpBeforeClass()
{
logger.log(Level.INFO, "setUpBeforeClass");
}

/**
* 只有在类运行结束后执行一次
* 一般用于做类级别的资源回收
*/
@AfterClass
public static void tearDownBeforeClass()
{
logger.log(Level.INFO, "tearDownBeforeClass");
}

/**
* 在每个test方法执行前执行一次
* 可以用于方法级别的数据初始化
* 如果耗费资源较大,资源较多
* 那么会大大降低整个测试类的执行效率
*/
@Before
public void setUp()
{
logger.log(Level.INFO, "setUp");
arithmetic = new Arithmetic();
}

/**
* 在每个test方法执行后执行一次
* 可以用于方法级别的资源回收还原
*/
@After
public void tearDown()
{
logger.log(Level.INFO, "tearDown");
}

/**
* 真正干活的测试方法,其他方法都是最终为它而服务的
* 对Arithmetic.getParmaerOne()方法进行测试
*/
@Test
public void test_getParmaerOne_1()
{
logger.log(Level.INFO, "test_getParmaerOne_1");
Random random = new Random();

try
{
// 通过反射 获得Arithmetic类的parmaerOne字段,并随机赋值
Field field = arithmetic.getClass().getDeclaredField("parmaerOne");
field.setAccessible(true);
int expected = random.nextInt(Integer.MAX_VALUE);
field.setInt(arithmetic, expected);

// 断言,如果 实际值 != 期待值 则断言失败,显示message
Assert.assertEquals("如果断言失败,则显示此信息", expected, arithmetic.getParmaerOne());
}
catch (Exception e)
{
e.printStackTrace();
Assert.fail(e.getMessage());
}
}

/**
* 对于单体测试多会使用反射,而且还会要求测试代码覆盖率
* 但是对于功能性测试而言,则侧重实际结果
*/
@Test
public void test_getSum_1()
{
logger.log(Level.INFO, "test_getSum_1");
Random random = new Random();
int parm1 = random.nextInt(Integer.MAX_VALUE / 10);
int parm2 = random.nextInt(Integer.MAX_VALUE / 10);
int sum = parm1 + parm2;

arithmetic.setParmaerOne(parm1);
arithmetic.setParmaerTwo(parm2);

// 断言,如果 实际值 != 期待值 则断言失败,显示message
Assert.assertEquals("如果断言失败,则显示此信息", sum, arithmetic.getSum());
}

/**
* 由于增加了Ignore标签,所以此test case不会被执行
*/
@Ignore("可以加上此test case被ignore的原因")
@Test
public void test_IgnoreFlag_1()
{
logger.log(Level.INFO, "test_IgnoreFlag_1");
}

/**
* 对异常的测试,可以判断异常的类型
* 但是没法判断异常的message,所以很多时候还得用try catch
*/
@Test(expected = ArithmeticException.class)
public void test_Exception_1()
{
logger.log(Level.INFO, "test_Exception_1");
int i=1/0;
}

/**
* 防止测试case出现超时现象
* 如果超过1000ms测试case还没有执行结束
* 那么直接断言失败
*/
@Test(timeout = 1000)
public void test_Timeout_1()
{
logger.log(Level.INFO, "test_Timeout_1");
while(true);
}
}


附件为工程源码,测试目标类与测试代码类存在于不同工程,所以需要将两个工程之间建立依赖关系
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值