基于spring和mybatis项目的JUnit测试用例的实现

主要目的:实现JUnit的Crud
项目目前情况:spring+mybatis
想在前后端分离的情况下,
后端实现各个模块CRUD的junit
遇到的最大问题先是注入之后提示nullPointException
接着很快反应过来
是junit运行单个文件的时候并没有在启动容器,也就是说单独执行一个文件没有初始化spring,也就无法使用service了
那么如何才能初始化spring呢?这就是这次junit中遇到的最大的问题

@Autowired
public static ProjectService projectService;

package test;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import project.dao.ProjectDao;
import project.dao.ProjectService;
import project.domain.Project;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

//import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by san-pc on 2016/2/26.
 */
public class ProjectTestbeat1 {

   /* @Autowired
    public static ProjectDao projectDao;
*/
   @Autowired
   public static ProjectService projectService;
    @BeforeClass
    public static void init() {//junit之前init spring
        ApplicationContext   context = new ClassPathXmlApplicationContext("classpath*:conf/root.xml");//这里路径之前没有配对于是一直出错
        projectService = (ProjectService)context.getBean("projectService");
    }
    @Test
    public  void addProject() throws Exception {
        Project project = new Project();
        project.setProjectName("七天通知贷");
        project.setProjectCode("20151601032046");
        project.setProjectType("通知贷");
        project.setInterestDate(7);
        project.setInterestRatio(0.0009);
        project.setCompanyId(1);
        project.setCompanyName("旺金控股公司");
        project.setLinkmanId(Long.valueOf(16292 + ""));
        project.setLinkmanName("胡二");
        project.setCreateId(16309);
        project.setCreaterName("罗星星");
        project.setBudgetIncome(12000);
        project.setBudgetExpenses(1000);
        project.setRealExpenses(2000);
        project.setCreateDate(new Date());//创建日期
        project.setEditDate(new Date());//最后修改时间
        project.setPstartDate(new Date());//竞标日期
        project.setPfullDate(new Date());//满标日期
        project.setPvalueDate(new Date());//起息日期
        project.setPendDate(new Date());//回款日期
        project.setPrealendDate(new Date());//实际回款日期
        project.setRemark("测试数据");
        project.setStateCode("0");
        projectService.insertProject(project);
        projectService.updateProject(project);

    }

    public static void main(String[] args){
        try {
         //   addProject();
        } catch (Exception e) {
            System.out.println("---------------------添加错误--------------------");
            e.printStackTrace();
        }
    }
    public Map SELECT1(Map b) {
        return b;
    }

    // @Test
    public static Map JSON2Map() {
        System.out.println("shuchu");
        Map map = new HashMap();
        return map;
    }
}

上面代码中的
ApplicationContext context = new ClassPathXmlApplicationContext(“classpath*:conf/root.xml”);//
大家一定觉得好奇了,因为我们的配置文件肯定不止一个,如何把spirng和spring-mvc以及其他xml引入呢?
这个时候就需要写一个类似web.xml的文件包含所有需要的配置文件了
即:root.xml
基于spring和mybatis项目的JUnit测试用例的实现_第1张图片

接着spring的初始化就算完成了
接着是测试mapping的crud
首先是测试insert


第一部分是基本配置
"project.dao.ProjectDao">


    --表名 -->
    id="table_name">project

    id="table_roles">staff_roles

    id="select_fields">
               project_name,project_code, project_type, interest_date,interest_ratio,company_id,
                company_name,linkman_id,linkman_name,create_id,creater_name,budget_income,
                budget_expenses,real_expenses,create_date,edit_date,
               pstart_date, pfull_date,pvalue_date,pend_date,prealend_date,remark,state_code
    
    id="entityMap" type="project.domain.Project">
        <result column="project_name" property="projectName"  />
        <result column="project_code" property="projectCode" />
        <result column="project_type" property="projectType" />
        <result column="interest_date" property="interestDate" />
        <result column="interest_ratio" property="interestRatio" />
        <result column="company_id" property="companyId" />
        <result column="company_name" property="companyName"  />
        <result column="linkman_id" property="linkmanId" />
        <result column="linkman_name" property="linkmanName" />
        <result column="create_id" property="createId" />
        <result column="creater_name" property="createrName" />
        <result column="budget_income" property="budgetIncome" />
        <result column="budget_expenses" property="budgetExpenses" />
        <result column="real_expenses" property="realExpenses" />
        <result column="create_date" property="createDate"  />
        <result column="edit_date" property="editDate" />
        <result column="pstart_date" property="updatedAt" />
        <result column="pfull_date" property="operatorId" />
        <result column="pvalue_date" property="pvalueDate" />
        <result column="pend_date" property="pendDate"  />
        <result column="prealend_date" property="prealendDate" />
        <result column="remark" property="remark" />
        <result column="state_code" property="stateCode" />
    

第二部分是CRUD模块的实现
    --新增-->
    --keyProperty:指主键属性名,也是返回的数据
    useGeneratedKeys:表示设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中->
    id="insertProject" parameterType="project.domain.Project" keyColumn="project_id" useGeneratedKeys="true" keyProperty="project_id">
        INSERT INTO
        "table_name">
        (
        "select_fields" />
        )
        VALUES
        (
        #{projectName},#{projectCode},#{projectType},#{interestDate},#{interestRatio},#{companyId},
        #{companyName},#{linkmanId},#{linkmanName},#{createId},#{createrName},#{budgetIncome},
        #{budgetExpenses},#{realExpenses},#{createDate},#{editDate},
        #{pstartDate},#{pfullDate},#{pvalueDate}, #{pendDate}, #{prealendDate},#{remark},
        #{stateCode}
        )
    

最后测试结果(如下)新增成功,但是部分字段未保存成功,数据丢失问题,有空继续补全

这里写图片描述

你可能感兴趣的:(java/scala)