spring+ibatis用junit进行测试无法依赖注入:报错 -问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

spring+ibatis用junit进行测试无法依赖注入:报错

kun坤 2020-06-07 16:20:32 44

在创建单元测试类的时候提示我无法注入bean,全部报错信息如下

[2018-12-27 14:13:32,306] [main] [ERROR]  org.springframework.test.context.TestContextManager Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1077fc9] to prepare test instance [junitTest.SpringJunitTest@1cf4a2c]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'junitTest.SpringJunitTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: cn.com.xxx.ibatis.wx3.dao.configure.PzconfigDao junitTest.SpringJunitTest.pzconfigDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.com.xxx.ibatis.wx3.dao.configure.PzconfigDao] 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)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298) ~[spring-beans-3.2.16.RELEASE.jar:3.2.16.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148) ~[spring-beans-3.2.16.RELEASE.jar:3.2.16.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376) ~[spring-beans-3.2.16.RELEASE.jar:3.2.16.RELEASE]
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:127) ~[spring-test-2.5.jar:2.5]
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85) ~[spring-test-2.5.jar:2.5]
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231) ~[spring-test-2.5.jar:2.5]
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95) [spring-test-2.5.jar:2.5]
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139) [spring-test-2.5.jar:2.5]
	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61) [junit-4.8.2.jar:na]
	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54) [junit-4.8.2.jar:na]
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34) [junit-4.8.2.jar:na]
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44) [junit-4.8.2.jar:na]
	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52) [junit-4.8.2.jar:na]
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na]
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na]
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) [.cp/:na]
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na]
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: cn.com.xxx.ibatis.wx3.dao.configure.PzconfigDao junitTest.SpringJunitTest.pzconfigDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.com.xxx.ibatis.wx3.dao.configure.PzconfigDao] 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)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:531) ~[spring-beans-3.2.16.RELEASE.jar:3.2.16.RELEASE]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-3.2.16.RELEASE.jar:3.2.16.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:295) ~[spring-beans-3.2.16.RELEASE.jar:3.2.16.RELEASE]
	... 18 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.com.xxx.ibatis.wx3.dao.configure.PzconfigDao] 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)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:997) ~[spring-beans-3.2.16.RELEASE.jar:3.2.16.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:867) ~[spring-beans-3.2.16.RELEASE.jar:3.2.16.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779) ~[spring-beans-3.2.16.RELEASE.jar:3.2.16.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:503) ~[spring-beans-3.2.16.RELEASE.jar:3.2.16.RELEASE]
	... 20 common frames omitted
java.lang.NoSuchMethodError: org.junit.runner.notification.RunNotifier.testAborted(Lorg/junit/runner/Description;Ljava/lang/Throwable;)V
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:146)
	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
[2018-12-27 14:13:32,312] [Thread-1] [INFO]  org.springframework.context.support.GenericApplicationContext Closing org.springframework.context.support.GenericApplicationContext@1033a6f: startup date [Thu Dec 27 14:13:32 CST 2018]; root of context hierarchy
[2018-12-27 14:13:32,313] [Thread-1] [INFO]  org.springframework.beans.factory.support.DefaultListableBeanFactory Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e35bb7: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

个人怀疑是在加载配置文件的时候没有读取到spring管理bean的配置,但是项目正常启动是可以运行的,下图是我的测试类,不加@autowired单元测试可以通过,加了就通过不了

spring的配置文件如图

在web.xml中有配置spring配置文件的上下文

ibatis包中结构如下

劳烦各位大神,帮帮小弟,这到底是什么问题

Java 测试技术 Spring
分享到
取消 提交回答
全部回答(1)
  • kun坤
    2020-06-07 16:20:37

    来人啊来人啊 T-T

    ######

    大神们,帮帮忙啊

    ######

    你才是大神,ibatis都没人用了。

    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程