Spring整合Junit——Spring(4)

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

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章