---
title: "Spring框架和Junit的结合使用"
author: "西子湖畔的家"
date: "2016-03-02T20:37:21Z"
tags: ["Spring", "junit", "测试"]
categories: ["开源软件"]
slug: "spring-junit"
draft: false
lastmod: "2016-04-19T17:49:55Z"
id: 2280097
url: "https://wgfhit.iteye.com/blog/2280097"
---

在spring框架做测试时，需要将所使用到的bean全部加载进容器中才可以进行测试，采用spring框架和junit结合的方法进行测试，配置如下：

```
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:application.test.xml")
```

其中：@RunWith是指定使用的单元测试执行类

   @ContextConfiguration是需要加载的配置文件，其中locations是一个字符串数组：

```
  String[] locations() default {};
```

 因此支持一次加载多个配置文件。（这里是

```
classpath*:application.test.xml
```

）加载进来，即可加载其所包含的配置，并且可以像使用普通junit一样进行测试。