2014-01-09 62 views
0

我在maven項目中有一個在xml中定義的spring bean。它在src/main/java中使用pojo類。此xml文件當前存儲在src/main/resources中。當這一切都工作正常,它被注入作爲一種資源。從測試/資源注入spring bean找不到main/java的類

​​

但我想將XML文件移動到的src /測試/資源,但仍然有它的src/main/java的引用POJO,因爲它的測試數據。當我移動它來測試它拋出一個錯誤:

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

是否可以移動XML的Spring bean文件測試和使用主,如果是的話怎麼辦?

感謝

的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> 

Steps.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.springframewor[email protected]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! 
+0

請發佈完整的異常堆棧跟蹤。 –

+0

添加堆棧跟蹤 – user1355642

+0

我在堆棧跟蹤中看不到'CannotLoadBeanClassException'。 –

回答

0

所以在玩耍之後,我解決了這個問題。

TestBeans.xml文件被放在名爲applicationContext的子文件夾的src/test/resources中。

下面一行從appliationContext.xml了採豆中的src/main /資源完全沒

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

但這個bean不能訪問POJO org.testing.framework.model.TestModel

現在,當我進口改爲

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

它拿起豆精,像原線,但隨後的bean可以訪問POJO。我不清楚爲什麼它需要文件夾名稱,因爲無論哪種方式都可以很好地拾取文件。沒有文件夾名稱就無法訪問pojo。

有誰知道這是爲什麼?無論如何感謝您的看法。