您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
學(xué)習(xí)使用JUnit
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/1/2 16:12:16 ] 推薦標(biāo)簽:單元測試 JUnit

一般使用JUnit測試業(yè)務(wù)層,web層是無法進行單元測試,無法進行創(chuàng)建對象;調(diào)用get或post進行傳參。
使用方法:
1.對測試類右鍵new->other->JAVA->JUnit->Junit Test Case;選擇需要測試的函數(shù),并導(dǎo)入junit的包。
2.編寫測試代碼:
package com.bluedot.test.service;

import static org.junit.Assert.*;

import java.util.List;

import junit.framework.Assert;

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

import com.bluedot.domain.User;
import com.bluedot.service.UserManager;
import com.bluedot.service.UserManagerImpl;

public class UserManagerImplTest
{
    @Before//在測試代碼之前運行
    public void aaaa()
    {
        System.out.println("我在測試代碼之前運行!");
    }

    @Test
    public void testFindUserByProperty()
    {
        UserManager m = new UserManagerImpl();
        User user = new User();
        user.setLoginName("admin");
        List<User> list = m.findUserByProperty(user);

        // 斷言,對比查詢的結(jié)果是否正確
        assertEquals(2, list.size());
        User u = list.get(0);
        assertEquals("admin", u.getLoginName());
        assertEquals("admin", u.getPwd());
        assertEquals(new Long(1), u.getRole().getId());
    }

    @After//測試代碼之后運行
    public void bbbb()
    {
        System.out.println("我在測試代碼之后運行");
    }
}

3.運行junit,結(jié)果顯示綠條為正確,錯誤則顯示紅條,根據(jù)錯誤信息能準(zhǔn)確定位錯誤的位置。

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd