喜歡用Junit進行單元測試的時候,會碰到spring的配置文件無法映射,這樣的問題我也碰到過,后來找到了解決辦法,就是在類頭部要增加注解,因為用junit測試的時候,是沒有容器的放置相關配置,所以測試的時候會報空值錯誤。配置如下:
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })public class ServiceAttachmentServiceImplTest { @Resource private IServiceAttachmentService serviceAttachmentService; @Test//測試SQL語句 public void testSQL() { List<ServiceAttachment> atts = serviceAttachmentService .getScrollData().getResultList(); }之前一個朋友告訴我,這樣配置后還是錯誤的,后來朋友告訴我,spring的配置文件如果用表達式的方式填充properties屬性的話也是無效的,需直接把值寫進入配置文件,這個問題我沒試過,有碰到這種問題的朋友可以試試。
新聞熱點
疑難解答