Java接口自动化测试之集成MyBatis和MySQL

1、pom.xml新增dependency


            org.mybatis
            mybatis
            3.4.4
        
        
            mysql
            mysql-connector-java
            5.1.6

2、mybatis.xml




    
    
        
            
            
                
                
                
                
            
        
    
    
    
        
    

3、mapper文件SQLMapper.xml






    

    


4、DoGetCase.java / DoPostCase.java 等JavaBean, 根据被测接口参数设计字段, 在MySQL中创建同样的表, 表字段也是根据被测接口的参数设计

package com.testng.model;

import lombok.Data;

@Data
public class DoGetCase {
    private int id;
    private String name;
    private String password;
    private String expected;
}

5、公共类,连接数据用DatabaseUtil.java

package com.testng.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class DatabaseUtil {
    public static SqlSession getSqlSession() throws IOException {
        // 获取配置的资源文件
        Reader reader = Resources.getResourceAsReader("mybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        // sqlSession能够执行配置文件中的SQL语句
        SqlSession sqlSession = factory.openSession();
        return sqlSession;
    }
}

6、修改优化TestNG测试用例, 对于每个被测接口可以单独提取出来一个方法, 唯一的变量就是数据库用例的id编号, 这里就不演示了

package com.testng.cases;

import com.testng.model.DoGetCase;
import com.testng.utils.DatabaseUtil;
import com.testng.utils.HttpUtils;
import com.testng.utils.ReadConfig;
import org.apache.ibatis.session.SqlSession;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;

import java.io.IOException;

public class DoGetTest {

    @Test(description = "成功的案例")
    public void getTest() throws IOException {
        SqlSession session = DatabaseUtil.getSqlSession();
        DoGetCase doGetCase = session.selectOne("doGet", 1);
        String uri = ReadConfig.URI + ReadConfig.GET_PATH1;
        String param = "name=" + doGetCase.getName() + "&password=" + doGetCase.getPassword();
        String url = uri + param;
        Reporter.log("请求地址" + url);
        String result = HttpUtils.doGet(url);
        Reporter.log("请求结果" + result);
        Assert.assertEquals(result, doGetCase.getExpected());
    }

    @Test(description = "失败的案例")
    public void getTest2() throws IOException {
        SqlSession session = DatabaseUtil.getSqlSession();
        DoGetCase doGetCase = session.selectOne("doGet", 2);
        String uri = ReadConfig.URI + ReadConfig.GET_PATH2;
        String param = "name=" + doGetCase.getName() + "&password=" + doGetCase.getPassword();
        String url = uri + param;
        Reporter.log("请求地址" + url);
        String result = HttpUtils.doGet(url);
        Reporter.log("请求结果" + result);
        Assert.assertEquals(result, doGetCase.getExpected());
    }
}

你可能感兴趣的:(接口测试,自动化测试)