gpt4 book ai didi

java - 无法在 Spring 3.2.8 和 junit 4.4 中 Autowiring 现场执行 Junit 测试

转载 作者:行者123 更新时间:2023-11-30 08:01:50 25 4
gpt4 key购买 nike

有了这门课,我必须使用 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;

...
}

我收到这个奇怪的错误,说不可能注入(inject) 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 {
....
}

最佳答案

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

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

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

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

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

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

解决方案:
1) 在 GeolocationServiceImpl 上添加 @Component 注解,如下所示:

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

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

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

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

关于java - 无法在 Spring 3.2.8 和 junit 4.4 中 Autowiring 现场执行 Junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31805855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com