项目经验之springmvc单元测试
2016-03-30 10:50
537 查看
在SpringMVC的单元测试中,首先要对当前环境进行引入,比如:
@WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {NoticeServiceConfig.class})
错误情况:在NoticeServiceConfig类中如下:
@ComponentScan({"com.xx.xx.notice"})@EnableJpaRepositories( basePackages = {"com.xx.xx.common.repository"})@ImportResource("classpath:datasource_mysql.xml")
报错:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xx.xx.notice.repository.WeixinSenderRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
究其原因是因为我的dao层接口实现了JpaRepository接口,如果在单元测试中,就必须要引入该dao层所在的package,通过@EnableJpaRepositories指定到相应的包就可以,重点在于使用该注解,不能与@ComponentScan扫描的包的路劲重合,否则会提示:
Could not autowire. There is more than one bean of 'WeixinSenderRepository' type. Beans:weixinSenderRepository,weixinSenderRepository.
相关文章推荐
- Ruby单元测试框架TestUnit的替代者MiniTest介绍
- Android学习笔记之应用单元测试实例分析
- 详解Spring Boot Junit单元测试
- 我的Node.js学习之路(四)--单元测试
- Android编程单元测试实例详解(附源码)
- Android编程之单元测试实例分析
- Android应用开发中单元测试分析
- mstest实现类似单元测试nunit中assert.throws功能
- java编程之单元测试(Junit)实例分析(附实例源码)
- python单元测试unittest实例详解
- angularjs中的单元测试实例
- Python中unittest模块做UT(单元测试)使用实例
- Android单元测试 笔记
- Spring整合Dwr,Cannot locate BeanDefinitionParser for element [annotation-scan]
- 在 Ruby on Rails 中进行单元测试
- 单元测试工具Numega
- 用cpp做c++单元测试
- JUnit4概述
- 单元测试详解
- Python 6.2 单元测试