一般使用JUnit測試業(yè)務層,web層是無法進行單元測試,無法進行創(chuàng)建對象;調(diào)用get或post進行傳參。
使用方法:
1.對測試類右鍵new->other->JAVA->JUnit->Junit Test Case;選擇需要測試的函數(shù),并導入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ù)錯誤信息能準確定位錯誤的位置。