2014-02-19 52 views
19

这里是我使用的文件:如何使用Spring Autowire编写JUnit测试?

component.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:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"> 

    <context:component-scan 
     base-package="controllers,services,dao,org.springframework.jndi" /> 
</beans> 

ServiceImpl.java

@org.springframework.stereotype.Service 
public class ServiceImpl implements MyService { 

    @Autowired 
    private MyDAO myDAO; 

    public void getData() {...}  
} 

ServiceImplTest.java

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("classpath*:conf/components.xml") 
public class ServiceImplTest{ 

    @Test 
    public void testMyFunction() {...} 
} 

错误:

16:22:48.753 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframewor[email protected]2092dcdb] to prepare test instance [[email protected]] 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'services.ServiceImplTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private services.ServiceImpl services.ServiceImplTest.publishedServiceImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [services.ServiceImpl] 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:287) ~[spring-beans.jar:3.1.2.RELEASE] 
+0

1.您的上下文配置提及'components.xml',但是你的文件叫做'Componet.xml'(可能只是在这篇文章中输入错误)2.你的Component.xml中没有为'PublishedReferenceYieldDAO'类定义的bean –

+0

也指定了组件扫描两次。一个在xml中,另一个在注释中。只需要其中一个。 – MystyxMac

+0

@DanTemple错误后..实际上我的文件名是component.xml – Premraj

回答

14

确保您导入了正确的软件包。如果我记得正确的话,Autowiring有两种不同的软件包。应该是:org.springframework.beans.factory.annotation.Autowired;

而且这看起来奇怪的对我说:

@ContextConfiguration("classpath*:conf/components.xml") 

这里是为我工作得很好的例子:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "/applicationContext_mock.xml" }) 
public class OwnerIntegrationTest { 

    @Autowired 
    OwnerService ownerService; 

    @Before 
    public void setup() { 

     ownerService.cleanList(); 

    } 

    @Test 
    public void testOwners() { 

     Owner owner = new Owner("Bengt", "Karlsson", "Ankavägen 3"); 
     owner = ownerService.createOwner(owner); 
     assertEquals("Check firstName : ", "Bengt", owner.getFirstName()); 
     assertTrue("Check that Id exist: ", owner.getId() > 0); 

     owner.setLastName("Larsson"); 
     ownerService.updateOwner(owner); 
     owner = ownerService.getOwner(owner.getId()); 
     assertEquals("Name is changed", "Larsson", owner.getLastName()); 

    } 
+0

1.正如你提到的导入包。我使用相同的2.我使用的框架文件夹“conf”包含components.xml和文件夹“app”包含java代码。 – Premraj

+0

如何只运行这个测试@PeterParker –

+0

* classpath *:conf/components.xml *是**全局命令**更多http://stackoverflow.com/tags/glob/info – Premraj

0

我觉得在某处你的代码是你@Autowiring具体类ServiceImpl,你应该自动装配它的界面(大概MyService)。

+0

仔细检查是否有其他实例,并且上下文实际上是使用实现类查找包。在这些情况下,在Spring中使用log4j并切换org.springframework包层次结构的调试会很有帮助 - 您可以查看它正在考虑的内容,然后执行操作。 – jmkgreen