在spring框架做測試時,需要將所使用到的bean全部加載進容器中纔可以進行測試,採用spring框架和junit結合的方法進行測試,配置如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:application.test.xml")
其中:@RunWith是指定使用的單元測試執行類
@ContextConfiguration是需要加載的配置文件,其中locations是一個字符串數組:
String[] locations() default {};
因此支持一次加載多個配置文件。(這裏是
classpath*:application.test.xml
)加載進來,即可加載其所包含的配置,並且可以像使用普通junit一樣進行測試。