例如我们有:
@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才会被虚拟机读取。