本文转载如下:
http://www.ibm.com/developerworks/cn/java/j-lo-spring25-test/index.html?ca=drs-
http://www.ibm.com/developerworks/cn/java/j-lo-springunitest/
以上两篇文章对Spring、JUnit4的集成介绍的非常详细,在此也就无须“重复造轮子”,把最终的配置贴出来,以备不时之需。
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
/**
* 此类为测试类的基类,所有测试类继承此类即可,无须再添加其他配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
//单个配置文件
//@ContextConfiguration(locations="classpath:applicationContext.xml")
//多个配置文件
@ContextConfiguration(locations={
"classpath:/conf/applicationContext-resource.xml",
"classpath:/conf/applicationContext-datasourceSpry.xml",
"classpath:/conf/applicationContext-hibernate.xml"})
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true)
@Transactional
public class BaseDaoTest {
@BeforeClass
public static void setUp(){
//init context environment
}
@AfterClass
public static void tearDown(){
//clean context environment
}
}