2011-11-17 110 views
0

我现在有一个恼人的问题。 我的测试因自动导线而失败。春季3 @测试中的自测

无法自动装配领域:私人k.dao.CompanyDao k.dao.CompanyDaoTest.companyDao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合依赖关系的[k.dao.CompanyDao]类型的匹配bean:期望至少1个符合此依赖关系自动装配候选资格的bean。

我觉得@ContextConfiguration可以是问题吗?

测试

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:**/servlet-context.xml", "classpath:**/root-context.xml", "classpath:**/ccc-jpa.xml" }) 
public final class CompanyDaoTest { 

    @Autowired 
    private CompanyDao companyDao; 

    @Test 
    public void testTest() { 

    } 
} 

CompanyDao

public interface CompanyDao extends GenericDao<Company> { 

} 

CompanyDaoJpa

@Repository("companyDao") 
public class CompanyDaoJpa extends GenericDaoJpa<Company> implements CompanyDao { 

    public CompanyDaoJpa() { 
     super(Company.class); 
    } 
} 

GenericDao

public interface GenericDao<T extends DomainObject> { 

    public T get(Long id); 

    public List<T> getAll(); 

    public T save(T object); 

    public T delete(T object); 

} 

的servlet-context.xml的

<annotation-driven /> 

    <context:component-scan base-package="k"/> 
+0

其他Spring XML配置是什么样的? – tobiasbayer

回答

5

我猜你的测试不加载servlet-context.xml的。

您参考servlet-context.xml的类路径的资源,但是servlet-context.xml通常位于WEB-INF下,这是不是应用程序类路径的一部分。请注意,Spring在引用通配符(classpath:**/servlet-context.xml)时不会抱怨丢失的配置文件,因此即使无法找到配置文件,您的测试也将默默启动。

没有好的方法来访问单元测试中位于WEB-INF的Spring xml文件。如果您想针对它们运行测试,则需要将它们移动到类路径中(即,根据您的项目布局,可以将它们移动到srcresources之类的东西)。由于DispatcherServletContextLoaderListener希望在WEB-INF下找到这些文件,因此您还需要使用它们各自的contextConfigLocation参数重新配置它们。例如,在DispatcherServlet的情况下:

<init-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:**/servlet-config.xml</param-value> 
</init-param> 
+0

谢谢,那是问题:) – Kronis