开发者社区> 问答> 正文

Spring 单元测试出错 NoSuchBeanDefinitionException? 报错

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>

诚挚求教各位,谢谢!

展开
收起
优选2 2020-06-04 21:18:33 1350 0
1 条回答
写回答
取消 提交回答
  • InvestorService 类有没有加上spring的注解?

    2020-06-05 11:37:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
用AI 高效测试移动应用 立即下载
移动互联网测试到质量的转变 立即下载
给ITer的技术实战进阶课-阿里CIO学院独家教材(四) 立即下载