开发者社区> 问答> 正文

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 341 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

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

    2020-06-02 13:41:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载