Spring+Hibnate注解编程单元测试

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

//使用@RunWith(SpringJUnit4ClassRunner.class),才能使测试运行于Spring测试//环境    
@RunWith(SpringJUnit4ClassRunner.class)    
//@ContextConfiguration 注解有以下两个常用的属性:    
//locations:可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件    
//inheritLocations:是否要继承父测试类的 Spring 配置文件,默认为 true 
@ContextConfiguration(locations={"classpath:spring/spring-config-business.xml"})  
public class TestRisk extends AbstractTransactionalJUnit4SpringContextTests{
	
	@Autowired    
    private IRiskBO bo; 
	
	@Test
	@Transactional
	@rollback(false)
    public void Test()    
    {    
       Risk risk = bo.findRiskById("CW01", true);
       System.out.println(risk.getName()+":"+risk.getCode());
    }  
}

相关推荐