最近在進行Spring的回爐學習在通過 @RunWith和@ContextConfiguration 進行Spring上下文進行測試
具體代碼如下:
@RunWith(SpringRunner.class)
@ContextConfiguration(locations= {"classpath:ioc-helloworld-context.xml"})
public class IocHelloWorldAnnotationTest {
@Autowired
private IocHelloWorldXml iocHelloWorldXml;
@Autowired
private IocHelloWorldAnnotation iocHelloWorldAnnotation;
@Test
public void sayXml() {
iocHelloWorldXml.say();
}
@Test
public void sayAnnotation() {
iocHelloWorldAnnotation.say();
}
}
執行測試用例時候報 Method ‘initializationError’ not found.Opening test class 錯誤。
通過點擊SpringRunner 查看源碼發現有如下注釋:
如果想使用 SpringRunner 進行啓動junit 必須是4.12 或者更高的版本才行。自己spring 項目junit 版本是4.10所以報錯。果斷切換成4.12後就沒有問題了。