shop--5.使用Junit进行项目框架的验证

1.验证Dao层

1)创建AreaDao的接口(里面是对数据的增删改查方法)

2)创建AreaDao.xml(里面是上面AreaDao接口中方法调用的实际sql语句,与数据库关联,对数据进行实际的操作)

3)在test.java.com.shop下创建一个BaseTest类(配置Spring和Junit整合,Junit启动时加载springIOC容器)

/**
 * 配置Spring和Junit整合,Junit启动时加载springIOC容器
 */
@RunWith(SpringJUnit4ClassRunner.class)
//告诉Junit Spring的配置文件的位置
@ContextConfiguration({"classpath:spring/spring-dao.xml"})
public class BaseTest {
}

  

4)后面的AreaDAOTest测试类要继承BaseTest,每次都要到BaseTest中加载Spring的配置文件

5)然后在AreaDAOTest测试类中编写测试代码

Dao层

AreaDao接口

@Repository
public interface AreaDao {

    public List<Area> queryArea();
}

 

AreaDao.xml

<mapper namespace="com.shop.dao.AreaDao">
    <select id="queryArea" resultType="com.shop.bean.Area">
        SELECT area_id, area_name, priority,
        create_time, last_edit_time
        FROM area
        ORDER BY priority DESC
    </select>

</mapper>

  

service层

AreaService接口

public interface AreaService {

    public List<Area> getAreaList();
}

  

AreaServiceImpl实现类

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaDao areaDao;

    @Override
    public List<Area> getAreaList() {
        return areaDao.queryArea();
    }
}

  

controller层

 AreaController类

@Controller
@RequestMapping("/superadmin")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @RequestMapping(value="/listarea", method=RequestMethod.GET)
    //将返回对象自动转换为json对象给前端
    @ResponseBody
    private Map<String, Object> listArea(){
        Map<String, Object> modelMap = new HashMap<>();
        List<Area> areaList = new ArrayList<>();
        areaList = areaService.getAreaList();
        modelMap.put("rows", areaList);
        modelMap.put("total", areaList.size());

        return modelMap;
    }
}

  

 

原文地址:https://www.cnblogs.com/SkyeAngel/p/8870079.html