在使用springboot对持久层的方法进行单元测试时,出现错误。
错误一的错误代码如下:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'cn.tedu.storeExec.StoreExecApplicationTests':
Unsatisfied dependency expressed through field 'userMapper';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'cn.tedu.storeExec.mapper.UserMapper' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'cn.tedu.storeExec.mapper.UserMapper' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
上面的错误代码:No qualifying bean of type 'cn.tedu.storeExec.mapper.UserMapper' available,中文意思是说:没有符合的bean可以使用。就是说UserMapper不能被使用。
我的错误原因是没有添加组件扫描而导致的,在springboot的含有main方法的类中添加组件扫描的注解,如下:
添加完成之后,再次启动,进行单元测试,结果成功插入数据到数据库中: