首页 > 代码库 > 搭建spring测试环境

搭建spring测试环境

<dependency>  
      <groupId>org.springframework</groupId>  
      <artifactId>spring-test</artifactId>  
      <version>${spring.version}</version>  
      <scope>test</scope>  
</dependency>

添加一个父类,所有测试的类继承了这个类就可以用spring的注解。

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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:jdbc_beans.xml" })
public class SpringTest extends AbstractTransactionalJUnit4SpringContextTests {

}

eg. 写测试类:

/**
 * @author 李永
 * 
 */
public class StudentTest extends SpringTest {

	@Autowired
	StudentDao studentDao;
	@Autowired
	CourseDao courseDao;
	@Autowired
	MyInsert insert;
}








搭建spring测试环境