---
title: "junit浅学笔记二"
author: "eggbucket"
date: "2012-02-02T11:30:29Z"
tags: []
categories: ["开源软件"]
slug: "junit"
draft: false
lastmod: "2012-02-02T11:31:00Z"
id: 1390180
url: "https://eggbucket.iteye.com/blog/1390180"
---

上一节初步介绍了什么是单元测试，为什么要做单元测试，以及junit4的初步使用，这里我们接着说一下junit4中的注解。

  =============本节知识点============================

*     Error和Failures

*    Junit4 Annotation

==================================================================

 

1.   在讲注解之前，先来认识 Error和Failures这两种错误有什么不同。

 

Errors：表示程序本身错误

 

 

```
@Test
    publicvoid testAdd() {
        int z=new T().add(5,3);
        assertEquals(8,z);
        int a=8/0;  //这一句是有错误的
    }
```

 

运行方法，会有一下错误提示：

![](http://pic002.cnblogs.com/images/2011/311516/2011090221500319.png)

Failures: 是指测试失败。

```
@Test
    publicvoid testAdd() {
        int z=new T().add(5,4); //这里修改了数值，把4该为3就正确了 
        assertEquals(8,z);
    }
```

在来运行这个方法，看一下错误提示：

![](http://pic002.cnblogs.com/images/2011/311516/2011090221573748.png)

所以，我们在写测试程序的时候，要先保证Errors是没有错误的，再来看Failures有没有错误。

 

2.  下面介绍junit4 的常用注解

\-----------------------------------------------------------------------------------------------

*             @ Test：测试方法

                  A)  (expected=XXEception.class)

                B)  (timeout=xxx)

\*.           @ Ignore: 被忽略的测试方法

\*.           @Before: 每一个测试方法之前云行。

\*.           @After : 每一个测试方法之后运行。

\*.           @BefreClass 所有测试开始之前运行。

\*.           @AfterClass 所有测试结果之后运行。

\------------------------------------------------------------------------------------------------

下面通过一个测试程序来解释这些注解含义

```
package com.junit4.cc.test;

importstatic org.junit.Assert.*;
importstatic org.hamcrest.Matcher.*;

import org.junit.Test;
import com.junit4.cc.*;

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


publicclass TTest {
    
    @BeforeClass  //的所有方法运行之前运行。
publicstaticvoid beforeClass(){
        System.out.println("------------beforeClass");
    }
    
    @AfterClass   //在所有方法运行之后运行
publicstaticvoid afterClass(){
        System.out.println("-------------afterClass");
    }
    
    @Before   //每个测试方法运行之前运行
publicvoid before(){
        System.out.println("=======before");
    }
    
    @After   //每个测试方法运行之后运行
publicvoid after(){
        System.out.println("=======after");
    }

    @Test
    publicvoid testAdd() {
        int z=new T().add(5,3);
        assertEquals(8,z);
        System.out.println("test Run through");
    }
    
    @Test ()
    publicvoid testdivision(){
              System.out.println("in Test Division");

    }

    @Ignore   //表示这个方法是不被运行的
    @Test
    (expected=java.lang.ArithmeticException.class,timeout=100) //timeout表示要求方法在100毫秒内运行完成，否则报错
publicvoid testDivide(){
        int z =new T().divide(8,2);
    }
    
    

}
```

运行结果如下：

![](http://pic002.cnblogs.com/images/2011/311516/2011090222324512.png)

标记红星（\*）方法在每个方法开始和结尾都运行一次。

标记绿星（\*）的方法只在所有方法的开始和结尾运行一次。

junit有多种注解，我们常用的也就上面几种。