spring+mybatis用junit进行测试无法依赖注入

Ctony_ 2016-07-22 10:54:24
报错提示:

Error creating bean with name 'com.spring.test.UserServiceTest': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.spring.service.IUserService com.spring.test.UserServiceTest.userService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.IUserService] 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)}

测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
(locations={"classpath*:config/spring/springmvc.xml","classpath*:config/spring/applicationContext-*.xml","classpath*:config/mybatis/sqlMapConfig.xml"})
public class BaseJunit4Test {
}


public class UserServiceTest extends BaseJunit4Test {
private static Logger logger = Logger.getLogger(UserServiceTest.class);
long startruntime = 0;
long endruntime = 0;

@Before
public void before(){
startruntime=System.currentTimeMillis(); //获取开始时间
}
@After
public void after(){
endruntime=System.currentTimeMillis(); //获取开始时间
logger.info("此次方法运行时间: "+(endruntime-startruntime)+"ms");
}

@Autowired
private IUserService userService;

@Test
public void loginTest() throws Exception {
User user = userService.login("admin", "admin", null);
logger.info(user.toString());
}
}

去掉

@Autowired
private IUserService userService;

修改LoginTest()

@Test
public void loginTest() throws Exception {
logger.info("test");
}

无注入能够正常运行测试,@Autowired注入就会报错Error creating bean
...全文
228 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xigua1102 2016-07-22
  • 打赏
  • 举报
回复
配置文件没加载进去吧。。。
大雨将至 2016-07-22
  • 打赏
  • 举报
回复
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.IUserService] 看提示是spring认为com.spring.service.IUserService有多个实现类,不知道该注入哪个

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧