SSH框架学习(九、Junit4单元测试)

来源:互联网 发布:国际象棋棋谱制作软件 编辑:程序博客网 时间:2024/04/29 09:56

框架完成,开始一点一点添加其他内容。

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代码如下

package demo.myssh.dao.impl;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.test.annotation.Repeat;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.*;import demo.myssh.dao.IUserDAO;import demo.myssh.model.User;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"file:WebRoot/WEB-INF/applicationContext.xml"})public class UserDAOImplTest {@Autowired@Qualifier("user")private User user;@Autowired@Qualifier("iUserDAO")private IUserDAO userDao;@Beforepublic void setUp() throws Exception {user.setEmail("1email");user.setLoginName("1login name");user.setPassword("1assword");}@Test@Repeat(5)public final void testSave() {userDao.save(user);//fail("Not yet implemented"); }}

选择文件,run as -- junit test,

简单的测试,这样就算ok了。