JUnit单元测试 junit测试spring

第一篇关于网页信息抓取的日志看来帮到大家好多忙了 自己也很开心

测试课 JUnit,我把自己的总结写一下,也为了记录下可能以后用得着

测试程序NextDay, 环境EClipse Galileo



CalendarUnit抽象类 , Year Month Day 3个子类都继承该抽象类,分别代表 年,月,日

Date是日期类,包含 Year Month Day私有对象各一个,和其他日期操作若干

(CalendarUnit抽象类中的方法是要被继承到子类中去实现,所以这些方法放在子类中测试)

1.导入Referenced Libraries里面的那两个Junit.jar包

(将相关.jar包放到项目的lib文件夹下右击项目->Build Path->Configure 【】BuildPath->ADD Jars-> JUnit)

2.在junit 下的 default package下新建 Junit Test case文件,

对Year这个类测试, Next ,勾选需要测试的方法


import static org.junit.Assert.*;

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


public class Year1test {

@Before
public void setUp() throws Exception {
}

@Test
public void testIncrement() {
fail("Not yetimplemented");
}

@Test
public void testIsValid() {
fail("Not yetimplemented");
}

@Test
public void testYear() {
fail("Not yetimplemented");
}

生成的Year1Test文件如上, @Before @Test 是animation,JUnit4的新特性
@Test表示接下来的是需要运行的测试方法Junit,所以测试函数名如testYear()可以随意更改

以下是没有用到animation的(仅为了做对比,貌似是JUnit3的)

JUnit以 testXXX方法名 来告诉编译器这个是需要运行的测试方法

setup方法是初始化数据的,例如所有的测试用例都需要初始化某个变量,就可以提取到这个方法里来

import junit.framework.*;
public class TcasTest extends TestCase{


public void setUp() throws Exception{
}

public void testInitialize() {
Tcas.initialize();
assertTrue(Tcas.Positive_RA_Alt_Thresh[0]== 400&&
Tcas.Positive_RA_Alt_Thresh[1]== 500 &&
Tcas.Positive_RA_Alt_Thresh[2]== 640 &&
Tcas.Positive_RA_Alt_Thresh[3]== 740);
}

public void testALIM() {
assertEquals("这次测试",Tcas.Positive_RA_Alt_Thresh[2]
,640);
}
}

3.对Year Month Day Date分别如上新建测试类,在各个测试方法内调用需要被测试的方法,利用assert断言机制(assertEquals,assertTrue等)判断实际运行结果(或返回值)与预期值是否一致。

4.

现在有了YearTest MonthTest DayTest DateTest 4个测试类

现在右击junit->run as->Junit可以通过Junit来运行这些测试用例,但run as->javaapplication的话会报出错误,因为以javaapplication运行需要一个main()函数

所以再新建一个NextdayTest的类,写一个main函数,作为整个测试程序的入口

import junit.framework.*;

public class NextdayTest extends TestCase{
public static Test suite(){
TestSuite suite= new TestSuite();
suite.addTestSuite(YearTest.class);
suite.addTestSuite(MonthTest.class);
suite.addTestSuite(DayTest.class);
suite.addTestSuite(DateTest.class);

return suite;
}

public static void main(String[] args){
// 文本方式
junit.textui.TestRunner.run(suite());
}

}
将其他几个分类的测试作为suite 放到一个测试集中,

这样右击junit->run as ->javaapplication,也能运行了


ps:一些具体测试方法的技巧

(1)对异常的测试


public void testsetMonth1(){
try{
month.setMonth(3,new Year(2012));
assertTrue(true);
}catch(IllegalArgumentExceptionex){
fail("可惜 情人节 抛出异常了");
}
}

public void testsetMonth2(){
try{
month.setMonth(-1,new Year(2012));
fail("可惜情人节抛出异常了");
}catch(IllegalArgumentExceptionex){
assertTrue(true);
}
}

(2)一个方法输出 东西到控制台,判断输出的东西跟预期是否相同

输出重定向

  1. @Test
  2. public void test(){
  3. ByteArrayOutputStream baos = newByteArrayOutputStream();
  4. System.setOut(newPrintStream(baos));
  5. Date date = newDate(12,18, 2011);
  6. date.printDate();
  7. assertEquals("12/18/2011" +System.getProperty("line.separator"),baos.toString());
  8. }

(3)junit下的default package下的测试类方法 可以直接调用src下default package下文件的 public protected方法,因为都为defaultpackage,编码之后在同一个二进制文件中

  

爱华网本文地址 » http://www.aihuau.com/a/25101016/319808.html

更多阅读

白盒测试与单元测试 单元测试属于白盒测试

这几天需要跟dev结对编写一些单元测试,来验证我们系统中一些复杂的逻辑是否正确。回家的路上跟几个同事聊起单元测试和白盒测试,没有人能清晰解释它们的区别,自己也很模糊,所以回来就查了一些资料,作以下记录。(资料来自网络)单元,集成,确认

声明:《JUnit单元测试 junit测试spring》为网友妖娆宅男分享!如侵犯到您的合法权益请联系我们删除