【问题标题】:Could not autowire field performing Junit Tests in Spring 3.2.8 & junit 4.4无法在 Spring 3.2.8 和 junit 4.4 中自动装配现场执行 Junit 测试
【发布时间】:2015-08-04 09:43:42
【问题描述】:

有了这个类,我必须使用库 junit 4.4 执行一些测试。没有成功

public class GeolocationServiceTest extends AbstractAnnotationAwareTransactionalTests {

        private static final String GEOJSON_FILE =  "geojson_demo.geojson";

        private static final String GEOJSON_FILE2 =  "geojson_ecat.geojson";        

        private static final String ADDRESS  =  "rue commerce+PARIS";

        private static final String ADDRESS2 =  "Gravilliers 12, Paris";    


        private static final String[] CONFIG_LOCATIONS = new String[] {
            "classpath:com/devices/testServiceContext.xml",
            "classpath:com/devices/testApplicationContext.xml",      "classpath:com/devices/testDatabaseMessageSource.xml",     "classpath:com/devices/propertyeditorsContext.xml" };


        @Autowired
        private CompanyDao companyDao;

        @Autowired
        private GeolocationService geolocationService;

    ...
    }

我收到这个奇怪的错误,说不可能注入 bean,但是这个 bean 存在于文件 testServiceContext.xml 中,所以我根本不明白。

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.devices.services.GeolocationServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.devices.services.geolocation.GeolocationService com.devices.services.GeolocationServiceTest.geolocationService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.devices.services.geolocation.GeolocationService] 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:289)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
        at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:210)
        at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:184)
        at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:103)
        at junit.framework.TestCase.runBare(TestCase.java:139)
        at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:79)
        at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:74)
        at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:179)
        at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:287)
        at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:258)
        at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:176)
        at junit.framework.TestResult$1.protect(TestResult.java:122)
        at junit.framework.TestResult.runProtected(TestResult.java:142)
        at junit.framework.TestResult.run(TestResult.java:125)
        at junit.framework.TestCase.run(TestCase.java:129)
        at junit.framework.TestSuite.runTest(TestSuite.java:255)
        at junit.framework.TestSuite.run(TestSuite.java:250)
        at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.devices.services.geolocation.GeolocationService com.devices.services.GeolocationServiceTest.geolocationService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.devices.services.geolocation.GeolocationService] 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:517)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
        ... 25 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.devices.services.geolocation.GeolocationService] 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:988)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
        ... 27 more


here you can see the file testServiceContext.xml:

 <!-- Scan for services -->    
    <context:component-scan base-package="com.devices.services.impl" />
    <context:component-scan base-package="com.devices.services.geolocation" />

而且这个类似乎被很好地声明了

package com.devices.services.geolocation

@Service(value = "geolocationService")
public interface GeolocationService {
....    
}

【问题讨论】:

  • 请发布您的相关配置
  • 你到底是什么意思?

标签: java spring unit-testing spring-mvc junit


【解决方案1】:

您将在以下情况下收到此异常:

@Service(value = "geolocationService")
 public interface GeolocationService {
 ....    
 }

现在您已经创建了上述接口的实现

public class GeolocationServiceImpl implements GeolocationService {
..........
}

现在如果你尝试通过@Autowired 注入 GeolocationService,将会收到异常:

No qualifying bean of type   [com.devices.services.geolocation.GeolocationService] found for dependency

解决方案:
1)将@Component注释放在GeolocationServiceImpl上,如下所示:

@Component
public class GeolocationServiceImpl implements GeolocationService {
..........
}

2) 在spring-context.xml中定义为:-

<bean id="geolocationService"  class="com.devices.services.geolocation.GeolocationServiceImpl" />

简而言之,您需要让 Spring ApplicationContext 了解您的接口的实现。

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 2011-04-09
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多