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

在这里插入图片描述

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