搭建spring3+hibernate3测试环境

主要参考spring文档  

/spring-framework-3.1.2.RELEASE/docs/spring-framework-reference/html/testing.html

下面的  

Context configuration with XML resources的说明

不需要写configuration。。。。神马,也不用写getXXX,setXXX方法。

主要看文档,需要导入一个spring测试jar包
然后,
package com.anbang.hrsystem.dao;


import junit.framework.Assert;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import com.anbang.hrsystem.domain.AttendType;
@RunWith(SpringJUnit4ClassRunner.class)
//这里的两个配置文件applicationContext.xml文件和XXXdao文件都放在web应用的web-inf/下面了。必须加入这个file:来确定文件位置。慢慢调试。要是放在其他的位置,用class  或者value,使用eclipse的代码助手功能看下就知道了。
@ContextConfiguration(locations={"file:WebContent/WEB-INF/applicationContext.xml","file:WebContent/WEB-INF/daoContext.xml"})
public class AttendTypeDaoTest {
  
  @Autowired
  private AttendTypeDao attendTypeDao;


  @Test
  public void testGet() {
    AttendType type = (AttendType) attendTypeDao.get(AttendType.class, 1);
//   Assert.assertNotNull(type.getName());
    org.junit.Assert.assertEquals(type.getName(), "正常");
  }

发布了15 篇原创文章 · 获赞 3 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章