喜欢用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属性的话也是无效的,需直接把值写进入配置文件,这个问题我没试过,有碰到这种问题的朋友可以试试。
新闻热点
疑难解答