开发者社区 问答 正文

Spring 单元测试出错 NoSuchBeanDefinitionExcept?400报错

Spring 单元测试出错 NoSuchBeanDefinitionException? 400 报错

问题:程序可以正常启动并运行,但单元测试时则出错

出错提示:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.gwzyh.finance.services.InvestorService] 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)}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
	"classpath*:springmvc-servlet.xml"
})
@Transactional
public class InvestorServiceTest {
		
    @Autowired
    private InvestorService investorService;
		
    @Test
    public void investorServiceShouldBeInjected() {
	Assert.assertNotNull(investorService); 
    }
	
}



配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
       					   >
   	<context:annotation-config />                        					   
   	<context:component-scan base-package="com.gwzyh.finance">
   	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
   	</context:component-scan>
   <context:property-placeholder location="classpath*:properties/dev/jdbc.properties"/>
    <import resource="datasource-context.xml"/>
   
</beans>

诚挚求教各位,谢谢!

展开
收起
爱吃鱼的程序员 2020-06-02 13:41:46 368 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    InvestorService 类有没有加上spring的注解?

    2020-06-02 13:41:57 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等