架包支持:junit.jar;org.springframework.test-3.1.0.RELEASE.jar
  测试Dao基础类:JUnitDaoBase

 

package test.test;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <b>Summary: </b> TODO Junit 基础类,加载环境
* <b>Remarks: </b> TODO DAO测试基础类
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/config/applicationContext.xml"})
public  class JUnitDaoBase extends AbstractTransactionalJUnit4SpringContextTests {
/**
* <b>Summary: </b> 复写方法 setDataSource
*
* @param dataSource
* @see org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests#setDataSource(javax.sql.DataSource)
*/
@Override
@Resource(name = "myDataSource")
public void setDataSource(DataSource dataSource) {
// TODO Auto-generated method stub
super.setDataSource(dataSource);
}
}

  具体dao 测试继承daoBaseTest可以了

 

package test.java.dao;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.pinfang.dao.UserDao;
import com.pinfang.vo.User;
import test.test.JUnitDaoBase;
/**
* dao层测试例子
* @author fule
*
*/
public class UserDaoTest extends JUnitDaoBase {
@Autowired
private UserDao userDao;
@Test
public void testUserDao() {
User user = new User();
user.setPassword("123456");
user.setName("大妈");
user.setSex(1);
//System.out.println(userDao.findOnValidate(user).getMark());
List<User> list = userDao.getAll();
System.out.println(list);
//System.out.println(userDao.findOneByName("张三s"));
//System.out.println(userDao.addUser(user));
}
}