校园商铺-2项目设计和框架搭建-9验证Service

本文详细解析了在Spring框架中,AreaServiceTest测试类运行时出现的依赖注入失败问题,包括错误信息解读、可能的原因及解决方案,涉及Spring配置、依赖注入原理及常见问题排查。

1. 新建接口

main: com.csj2018.o2o.service/AreaService.java

package com.csj2018.o2o.service;
import java.util.List;
import com.csj2018.o2o.entity.Area;
public interface AreaService {
    List<Area> getAreaList();
}

2. 新建实现类

main: com.csj2018.o2o.service.impl/AreaServiceImpl.java

package com.csj2018.o2o.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csj2018.o2o.dao.AreaDao;
import com.csj2018.o2o.entity.Area;
import com.csj2018.o2o.service.AreaService;
@Service
public class AreaServiceImpl implements AreaService{
    @Autowired
    private AreaDao areaDao;
    @Override
    public List<Area> getAreaList(){
        return areaDao.queryArea();
    }
}

3. 修改基类

test: com.csj2018.o2o/BaseTest.java

package com.csj2018.o2o;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 配置spring和junit整合,junit启动式加载springIOC容器
 */
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring配置文件的位置
@ContextConfiguration({"classpath:spring/spring-dao.xml","classpath:spring/spring-service.xml"})
public class BaseTest {

}

4. 测试类

test: com.csj2018.o2o.service/AreaServiceTest.java

package com.csj2018.o2o.service;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.Area;

public class AreaServiceTest extends BaseTest{
    @Autowired
    private AreaService areaService;
    @Test
    public void testGetAreaList() {
        List<Area> areaList = areaService.getAreaList();
        assertEquals("南苑",areaList.get(0).getAreaName());
    }

}

1418970-20190923093006079-1961171163.png

问题:

1.AreaServiceTest运行错误

九月 21, 2019 6:21:41 下午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2d554825] to prepare test instance [com.csj2018.o2o.service.AreaServiceTest@26abb146]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.csj2018.o2o.service.AreaServiceTest': Unsatisfied dependency expressed through field 'areaService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.csj2018.o2o.service.AreaService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      ...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.csj2018.o2o.service.AreaService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 

原因:
1. spring-service.xml文件配置错误 ,修正即可
2. AreaServiceImpl.java是否缺少注释,如Controller注解

转载于:https://www.cnblogs.com/csj2018/p/11564003.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值