【问题标题】:Injecting spring bean from test/resources can't find class from main/java从测试/资源注入spring bean无法从main/java中找到类
【发布时间】:2014-01-27 11:09:37
【问题描述】:

我在一个 Maven 项目的 xml 中定义了一个 spring bean。它使用 src/main/java 中的 pojo 类。此 xml 文件当前存储在 src/main/resources 中。构建后一切正常,它作为资源注入。

   <bean id="test" class="org.testing.framework.model.TestModel">
      values here
   </bean>

但我想将 xml 文件移动到 src/test/resources 但仍然让它引用来自 src/main/java 的 pojo 作为其测试数据。当我移动它进行测试时,它会引发错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.testing.framework.model.TestModel]

是否可以移动xml spring bean文件来测试和使用main,如果可以,如何?

谢谢

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd">

    <import resource="classpath*:TestBeans.xml" />

</beans>

TestBeans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd">

    <bean id="test" class="org.testing.framework.model.TestModel">
       values....
    </bean>

    <util:map id="testMap" value-type="org.testing.framework.model.TestModel">
        <entry key="testKey" value-ref="test" />
    </util:map>

</beans>

步骤.java

package org.testing.framework.steps;

import java.util.Map;
import javax.annotation.Resource;
import net.thucydides.core.pages.Pages;
import org.springframework.stereotype.Component;
import org.testing.framework.model.TestModel;
import org.testing.framework.pages.XmlFilePath;

@Component
public class Steps extends AuatSteps {

    @Resource
    private XmlFilePath testXmlFilePath;

    @Resource
    private Map<String, TestModel> testMap;

    public Steps(Pages pages) {
        super(pages);
    }

    public String getXmlFilePath() {
        return testXmlFilePath.getXmlFilePath();
    }

    public Map<String, TestModel> getTestMap() {
        return testMap;
    }
}

堆栈跟踪:

1069 [main] ERROR org.springframework.test.context.TestContextManager  - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@233dcd27] to prepare test instance [WoWSteps$$EnhancerByCGLIB$$f625fb6]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.testing.framework.steps.Steps': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.testing.framework.model.TestModel] found for dependency [map with value type org.testing.framework.model.TestModel]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:379)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at net.thucydides.core.steps.SpringDependencyInjector.injectDependenciesInto(SpringDependencyInjector.java:16)
    at net.thucydides.core.steps.StepFactory.injectOtherDependenciesInto(StepFactory.java:124)
    at net.thucydides.core.steps.StepFactory.instantiateNewStepLibraryFor(StepFactory.java:114)
    at net.thucydides.core.steps.StepFactory.instantiateNewStepLibraryFor(StepFactory.java:100)
    at net.thucydides.core.steps.StepFactory.getStepLibraryFor(StepFactory.java:66)
    at net.thucydides.core.steps.StepAnnotations.instantiateAnyUnitiaializedSteps(StepAnnotations.java:50)
    at net.thucydides.core.steps.StepAnnotations.instanciateScenarioStepFields(StepAnnotations.java:41)
    at net.thucydides.core.steps.StepAnnotations.injectScenarioStepsInto(StepAnnotations.java:23)
    at net.thucydides.jbehave.ThucydidesStepFactory.createInstanceOfType(ThucydidesStepFactory.java:80)
    at org.jbehave.core.steps.StepCandidate.getStepsInstance(StepCandidate.java:68)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.addRegularStep(JUnitDescriptionGenerator.java:190)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.addExistingStep(JUnitDescriptionGenerator.java:148)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.addSteps(JUnitDescriptionGenerator.java:138)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.addStepsToExample(JUnitDescriptionGenerator.java:127)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.insertDescriptionForExamples(JUnitDescriptionGenerator.java:121)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.createDescriptionFrom(JUnitDescriptionGenerator.java:55)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.addAllScenariosToDescription(JUnitDescriptionGenerator.java:208)
    at de.codecentric.jbehave.junit.monitoring.JUnitDescriptionGenerator.createDescriptionFrom(JUnitDescriptionGenerator.java:44)
    at net.thucydides.jbehave.runners.ThucydidesReportingRunner.addStories(ThucydidesReportingRunner.java:265)
    at net.thucydides.jbehave.runners.ThucydidesReportingRunner.buildDescriptionFromStories(ThucydidesReportingRunner.java:245)
    at net.thucydides.jbehave.runners.ThucydidesReportingRunner.getDescriptions(ThucydidesReportingRunner.java:74)
    at net.thucydides.jbehave.runners.ThucydidesReportingRunner.getDescription(ThucydidesReportingRunner.java:120)
    at net.thucydides.jbehave.runners.ThucydidesReportingRunner.run(ThucydidesReportingRunner.java:145)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:234)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:133)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:114)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:188)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:166)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:101)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.testing.framework.model.TestModel] found for dependency [map with value type org.testing.framework.model.TestModel]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:967)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:824)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
    ... 40 more
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.633 sec <<< FAILURE!

【问题讨论】:

  • 请发布完整的异常堆栈跟踪。
  • 我在堆栈跟踪中没有看到CannotLoadBeanClassException
  • 我的错,从那以后我就一直在玩它。我将编辑主要错误。
  • K.现在我们需要查看您的上下文配置和需要它的测试。
  • 您是否完全复制了上下文文件,或者它是否有一些额外的配置,例如 tx:annotation-driven for @Transactional...

标签: java spring maven dependency-injection


【解决方案1】:

所以在玩了之后我解决了这个问题。

TestBeans.xml 文件被放置在 src/test/resources 的一个名为 applicationContext 的子文件夹中。

以下行完全可以从 src/main/resources 中的 appliationContext.xml 中提取 bean

<import resource="classpath*:TestBeans.xml" />

但是这个bean不能访问pojo org.testing.framework.model.TestModel

现在当我将导入更改为

<import resource="classpath*:applicationContext/TestBeans.xml" />

它可以很好地拾取 bean,就像原始行一样,但是 bean 然后可以访问 pojo。我不清楚为什么它需要文件夹名称,因为无论哪种方式都可以很好地拾取文件。它只是无法访问没有文件夹名称的 pojo。

有人知道为什么会这样吗?无论如何,感谢您的关注。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多