SSH框架學習(七、Junit4單元測試)

http://blog.csdn.net/wuyt2008/article/details/8275096

框架完成,開始一點一點添加其他內容。

myeclipse10自帶有junit4,直接用就好,當然如果要下載也行。https://github.com/KentBeck/junit/downloads

在之前的基礎上,我將dao和service層都改成了接口調用,其他沒變。


對UserDAO進行測試,在myeclipse裏面直接添加junit test case就好,然後再引入spring的test包:org.springframework.test-3.1.3.RELEASE

UserDAOImplTest代碼如下

[java] view plaincopy
  1. package demo.myssh.dao.impl;  
  2.   
  3. import org.junit.Before;  
  4. import org.junit.Test;  
  5. import org.junit.runner.RunWith;  
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.beans.factory.annotation.Qualifier;  
  8. import org.springframework.test.annotation.Repeat;  
  9. import org.springframework.test.context.ContextConfiguration;  
  10. import org.springframework.test.context.junit4.*;  
  11. import demo.myssh.dao.IUserDAO;  
  12. import demo.myssh.model.User;  
  13.   
  14. @RunWith(SpringJUnit4ClassRunner.class)  
  15. @ContextConfiguration({"file:WebRoot/WEB-INF/applicationContext.xml"})  
  16. public class UserDAOImplTest {  
  17.   
  18.     @Autowired  
  19.     @Qualifier("user")  
  20.     private User user;  
  21.       
  22.     @Autowired  
  23.     @Qualifier("iUserDAO")  
  24.     private IUserDAO userDao;  
  25.       
  26.     @Before  
  27.     public void setUp() throws Exception {  
  28.         user.setEmail("1email");  
  29.         user.setLoginName("1login name");  
  30.         user.setPassword("1assword");  
  31.     }  
  32.       
  33.     @Test  
  34.     @Repeat(5)  
  35.     public final void testSave() {  
  36.         userDao.save(user);  
  37.         //fail("Not yet implemented");   
  38.     }  
  39. }  

選擇文件,run as -- junit test,

簡單的測試,這樣就算ok了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章