首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

JUnit学习札记-3

2012-09-28 
JUnit学习笔记-31.学习了JUnit4.x中的@Test,@BeforeClass,@AfterClass,@Before,@After,@Ignore标签的意义,

JUnit学习笔记-3
1.学习了JUnit4.x中的@Test,@BeforeClass,@AfterClass,@Before,@After,@Ignore标签的意义,并且使用它们。并且学习了带expteced和timeout参数的@Test标签。

2.学习了JUnit中Failure和Error的区别。及其它们是怎样出现的。

3.温故了Java中Excepion和Error的知识,只有Excpetion和Error两个类实现了Throwable接口。发现Error能手动抛出和捕捉,虽然这样写是无任何意义的。
例:

 public class TestError{  public void throwError(){    throw new Error("抛出的错误");  }    public static void main(String[] args){    TestError te = new TestError();    try{      te.throwError();    }catch(Error e){      System.out.println("catch到了一个错误");      e.printStackTrace();    }  } }



4.下面是学习期间写的代码笔记:
  MathDemo类:提供加和除的方法
 
package com.yuchujin.junit;public class MathDemo {public int add(int a,int b){return a+b;}public int div(int a,int b){if(0 == b){throw new ArithmeticException("除数不能为0");}return a/b;}public Object getObject() {Object[] os = new Object[2];//1//return null;//2return os[3];}  public void throwError(){    throw new Error("抛出的错误");  }}


    MathDemoTest类:MathDemo的测试类
package com.yuchujin.junit;import static org.junit.Assert.*;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;/** * MathDemo测试类 * @author Ayuchujin * */public class MathDemoTest {MathDemo demo = null;//@BeforeClass注释:在类加载时初始化@BeforeClasspublic static void setUpBeforeClass(){System.out.println("setUpBeforeClass");}//@AfterClass注释:在类"销毁"时执行@AfterClasspublic static void treaDownAfterClass(){System.out.println("treaDownAfterClass");}//@Before注释:在对象生成时初始化@Beforepublic void setUp(){System.out.println("setUp");demo = new MathDemo();}//@After注释:代表在对象"销毁"时执行@Afterpublic void tearDown(){System.out.println("tearDown");demo = null;}//@Test注释:代表要测试的测试方法@Testpublic void testAdd() {int expected = 2;int actual = demo.add(1, 1);assertEquals(expected, actual);}@Testpublic void testDiv() {int expected = 2;int actual = demo.div(2, 1);//String作为第一个参数:在出错后显示第一个String消息信息assertEquals("除出错了",expected, actual);//没有继承任何类仍然可以使用类方法。原因是因为使用了JAVA5的新特性,静态导入。}//expected带异常参数:如果方法体内抛出跟expected同样的异常则测试通过,否则测试不通过。@Test(expected = ArithmeticException.class)public void testDiv2(){int expected = 2;int actual = demo.div(1, 0);assertEquals(expected, actual);//JUnit4.x中提出了异常参数,在3.8中只能用try-catch来实现。这样使用JAVA注释的方式比3.8中跟简便//下面是3.8版本如何测试抛出异常//int expected = 2;//try{//int actual = demo.div(1, 0);//assertEquals(expected, actual);//}catch(Exception e){//Assert.assertNotNull(e);//}}//timeout带超时时间参数:如果执行方法花费时超过timeout则执行不通过,否则通过。@Test(timeout=5)public void testAdd2(){try {Thread.sleep(10);//线程在sleep到第5毫秒时被中断,JUnit测试不通过。} catch (InterruptedException e) {e.printStackTrace();}int expected = 2;int actual = demo.add(1, 1);assertEquals(expected, actual);}//两个参数可以连用@Test(expected=ArithmeticException.class,timeout=100)public void testDiv3(){int expected = 2;int actual = demo.div(1, 0);assertEquals(expected, actual);}//@Ignore注释:代表先定义一个测试方法,但未写测试内容,留给以后写。这样使用了@Ignore以后将不会被执行//在TDD中,是先写测试方法再在写业务类中具体的方法。这样Ignore标签就相当有用@Test@Ignorepublic void testNoNameMethod(){System.out.println("该测试方法不执行");// TODO 未完成测试方法}//JUnit中Error和Failure的区别@Test public void testGetObject(){//执行demo.getObject()中的1则会出现Failure//执行demo.getObject()中的2则会出现ErrorObject o = demo.getObject();//Failure代表:在执行断言时得到的实际结果和预期的不一样。//Error代表:在执行断言前出现了预想不到的情况。Error执行不到断言部分。下面一句执行不了,107行就直接抛出了下标越界异常。assertNotNull(o);}}

热点排行