---
title: "Junit测试加载配置文件"
author: "zhoucl"
date: "2012-04-16T09:45:53Z"
tags: []
categories: ["编程语言"]
slug: "junit"
draft: false
lastmod: "2012-04-16T09:49:22Z"
id: 1487270
url: "https://zhoucl.iteye.com/blog/1487270"
---

对于JUnit单元测试，加载Spring等的配置文件的路径写法问题，不同路径下的加载写法方式不一样，这两天做了下简单的测试，特作如下的记录。  
1、直接放在WEB-INF目录下：

```
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
        "file:webapp/WEB-INF/conf/applicationContext-resources.xml"})    //这是将配置文件放在WEB-INF目录下
```

2、放在src目录下：

```
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
        "classpath:applicationContext-resources.xml"}) // 这是将配置文件放在classes目录即src目录下
```