---
title: "[#0x0047] 使用Spring TestContext测试框架需要注意的一点"
author: "Aoyi"
date: "2010-08-11T09:18:59Z"
tags: []
categories: ["企业架构"]
slug: "0x0047-spring-testcontext"
draft: false
lastmod: "2010-08-22T16:53:36Z"
id: 734182
url: "https://aoyi.iteye.com/blog/734182"
---

例如我们有：

```
@ContextConfiguration(locations = {"classpath:datasource.xml", "classpath:process-engine.xml"})
public class ProcessConfigurationManagerTest extends AbstractJUnit4SpringContextTests {
	……
}
```

在写@BeforeClass方法时，datasource.xml和process-engine.xml这两个文件并没有读取，所以不能实例化bean。也就是说这两个文件的读取在@BeforeClass之后。@BeforeClass方法中就不能使用这两个xml文件中配置的bean，否则直接是NullPointerException。

　　在@Before方法中就不会有这中情况。

　　遇到两次了，谨记。

 

PS.

　　一个annotation类型被定义为@Retent(RetentionPolicy.RUNTIME)后，它才是在运行时可见，当class文件被装载时被保存在class文件中的annotation才会被虚拟机读取。