主要參考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(), "正常");
}