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());
}
}
## 问题:
### 1.AreaServiceTest运行错误
```#log
九月 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注解