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")