步骤
- 导入SpringTest模块
- @ContextConfiguration注解指定Spring配置文件的位置 自动创建IOC容器
- @RunWith指定spring单元测试模块
- 直接使用@Autowired或@Resource注解注入需要测试的组件
1.1 导入Spring-test jar包
下载地址: https://mvnrepository.com/artifact/org.springframework/spring-test
maven: 在pom.xml中添加
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>