can'i用testng注入模拟对象(服务)

时间:2013-11-19 16:26:49

标签: spring testng mockito

这是我的stackTrace:

ERROR TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@175078b] to prepare test instance [cat.base.pse.ui.CercaPeticionsTest@12d15a9]

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)         在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)         在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)         在org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)         在org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:117)         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)         在java.lang.reflect.Method.invoke(Method.java:597)         at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)         at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)         在org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)         在org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)         at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)         在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)         在org.testng.TestRunner.privateRun(TestRunner.java:767)         在org.testng.TestRunner.run(TestRunner.java:617)         在org.testng.SuiteRunner.runTest(SuiteRunner.java:334)         在org.testng.SuiteRunner.run顺序(SuiteRunner.java:329)         在org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)         在org.testng.SuiteRunner.run(SuiteRunner.java:240)         在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:51)         在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:85)         at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)         at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)         在org.testng.TestNG.run(TestNG.java:1030)         在org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:217)         在org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:84)         在org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:92)         在org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)         在org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)         as.IPseService]:预计至少有1个匹配的bean         在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)         at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)         ......还有35个     FAILED CONFIGURATION:@BeforeClass springTestContextPrepareTestInstance     org.springframework.beans.factory.BeanCreationException:创建名称为' cat.base.pse.ui.CercaPeticionsTest'的字段时出错:字段自动装配失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:protected cat.base.pse.serveis.IPseService cat.base.pse.ui.CercaPeticionsTest.serveiPSE;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义[cat.base.pse.serveis.IPseService]类型的唯一bean:类型[interface cat.base.pse.serveis.IPseService]的不满意的依赖项:expected至少1个匹配的bean         at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)         在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)         在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)         在org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)         在org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:117)         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)         在java.lang.reflect.Method.invoke(Method.java:597)         at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)         at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)         在org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)         在org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)         at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)         在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)         在org.testng.TestRunner.privateRun(TestRunner.java:767)         在org.testng.TestRunner.run(TestRunner.java:617)         在org.testng.SuiteRunner.runTest(SuiteRunner.java:334)         在org.testng.SuiteRunner.run顺序(SuiteRunner.java:329)         在org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)         在org.testng.SuiteRunner.run(SuiteRunner.java:240)         在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:51)         在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:85)         at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)

1 个答案:

答案 0 :(得分:0)

我的问题是调用测试环境的方法。 enter image description here

所以applicationContext-gui-Test.xml不在root src / test / java / resources,所以我需要指定绝对路径:

旧的错误方式: @ContextConfiguration(locations="classpath*:applicationContext-gui-Test.xml")

Correcte方式:@ContextConfiguration(locations="classpath:cat/base/pse/ui/applicationContext-gui-Test.xml")