1. 前言
由于测试工程师更多的是关注方法是否能够成功执行,而对于spring框架可能不是很了解。所以,对于如下方式我们应该尽量通过框架实现
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService accountService = (IAccountService) ac.getBean("accountService");
2. 添加依赖
首先我们要添加一个spring-test的依赖(这个地方有个细节,需要注意的是,我们需要使用)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.14.RELEASE</version>
</dependency>
3. 添加注解
在测试类上添加两个注解:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:xxx.xml")