service的验证和dao的验证类似,不过需要注意的是,当我们在测试类中编写service的测试类的时候,需要在基类中引入spring-service的配置,不然系统会报错service没有注入
@ContextConfiguration({“classpath:spring/spring-dao.xml”, “classpath:spring/spring-service.xml”})
AreaService.java
package com.imooc.o2o.service;
import java.io.IOException;
import java.util.List;
import com.imooc.o2o.entity.Area;
public interface AreaService {
List<Area> getAreaList();
}
AreaServiceImpl.java
package com.imooc.o2o.service.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.imooc.o2o.dao.AreaDao;
import com.imooc.o2o.entity.Area;
import com.imooc.o2o.service.AreaService;
@Service
public class AreaServiceImpl implements AreaService {
@Autowired
private AreaDao areaDao;
@Override
public List<Area> getAreaList(){
return areaDao.queryArea();
}
}
AreaServiceTest.java
package com.imooc.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.imooc.o2o.BaseTest;
import com.imooc.o2o.entity.Area;
public class AreaServiceTest extends BaseTest{
@Autowired
private AreaService areaService;
@Test
public void testGetAreaList() {
List<Area> areaList = areaService.getAreaList();
assertEquals("西苑", areaList.get(1).getAreaName());
}
}