标签:用例 3.4 action data uri log test TE 连接数
pom.xml新增dependency
1 <dependency> 2 <groupId>org.mybatis</groupId> 3 <artifactId>mybatis</artifactId> 4 <version>3.4.4</version> 5 </dependency> 6 <dependency> 7 <groupId>mysql</groupId> 8 <artifactId>mysql-connector-java</artifactId> 9 <version>5.1.6</version> 10 </dependency>
mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 注册对象的空间命名 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- 注册映射文件:java对象与数据库之间的xml文件路径! --> <mappers> <mapper resource="mapper/SQLMapper.xml"/> </mappers> </configuration>
mapper文件SQLMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 命名空间mapper,如果有多个mapper文件,这个必须唯一 --> <mapper namespace="com.testng.model"> <select id="doGet" parameterType="Integer" resultType="com.testng.model.DoGetCase"> select * from dogetcase where id = #{id}; </select> <select id="doPost" parameterType="Integer" resultType="com.testng.model.DoPostCase"> select * from dopostcase where id = #{id}; </select> </mapper>
DoGetCase.java / DoPostCase.java 等JavaBean, 根据被测接口参数设计字段, 在MySQL中创建同样的表, 表字段也是根据被测接口的参数设计
1 package com.testng.model; 2 3 import lombok.Data; 4 5 @Data 6 public class DoGetCase { 7 private int id; 8 private String name; 9 private String password; 10 private String expected; 11 }
1 package com.testng.model; 2 3 import lombok.Data; 4 5 @Data 6 public class DoPostCase { 7 private int id; 8 private String name; 9 private String password; 10 private String expected; 11 }
公共类,连接数据用DatabaseUtil.java
1 package com.testng.utils; 2 3 import org.apache.ibatis.io.Resources; 4 import org.apache.ibatis.session.SqlSession; 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 8 import java.io.IOException; 9 import java.io.Reader; 10 11 public class DatabaseUtil { 12 public static SqlSession getSqlSession() throws IOException { 13 // 获取配置的资源文件 14 Reader reader = Resources.getResourceAsReader("mybatis.xml"); 15 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader); 16 // sqlSession能够执行配置文件中的SQL语句 17 SqlSession sqlSession = factory.openSession(); 18 return sqlSession; 19 } 20 }
修改优化TestNG测试用例, 对于每个被测接口可以单独提取出来一个方法, 唯一的变量就是数据库用例的id编号, 这里就不演示了
1 package com.testng.cases; 2 3 import com.testng.model.DoGetCase; 4 import com.testng.utils.DatabaseUtil; 5 import com.testng.utils.HttpUtils; 6 import com.testng.utils.ReadConfig; 7 import org.apache.ibatis.session.SqlSession; 8 import org.testng.Assert; 9 import org.testng.Reporter; 10 import org.testng.annotations.Test; 11 12 import java.io.IOException; 13 14 public class DoGetTest { 15 16 @Test(description = "成功的案例") 17 public void getTest() throws IOException { 18 SqlSession session = DatabaseUtil.getSqlSession(); 19 DoGetCase doGetCase = session.selectOne("doGet", 1); 20 String uri = ReadConfig.URI + ReadConfig.GET_PATH1; 21 String param = "name=" + doGetCase.getName() + "&password=" + doGetCase.getPassword(); 22 String url = uri + param; 23 Reporter.log("请求地址" + url); 24 String result = HttpUtils.doGet(url); 25 Reporter.log("请求结果" + result); 26 Assert.assertEquals(result, doGetCase.getExpected()); 27 } 28 29 @Test(description = "失败的案例") 30 public void getTest2() throws IOException { 31 SqlSession session = DatabaseUtil.getSqlSession(); 32 DoGetCase doGetCase = session.selectOne("doGet", 2); 33 String uri = ReadConfig.URI + ReadConfig.GET_PATH2; 34 String param = "name=" + doGetCase.getName() + "&password=" + doGetCase.getPassword(); 35 String url = uri + param; 36 Reporter.log("请求地址" + url); 37 String result = HttpUtils.doGet(url); 38 Reporter.log("请求结果" + result); 39 Assert.assertEquals(result, doGetCase.getExpected()); 40 } 41 }
1 package com.testng.cases; 2 3 import com.alibaba.fastjson.JSONObject; 4 import com.testng.model.DoPostCase; 5 import com.testng.utils.DatabaseUtil; 6 import com.testng.utils.HttpUtils; 7 import com.testng.utils.ReadConfig; 8 9 import org.apache.ibatis.session.SqlSession; 10 import org.testng.Assert; 11 import org.testng.Reporter; 12 import org.testng.annotations.Test; 13 14 import java.io.IOException; 15 16 public class DoPostTest { 17 18 @Test 19 public void postTest() throws IOException { 20 SqlSession session = DatabaseUtil.getSqlSession(); 21 DoPostCase doPostCase = session.selectOne("doPost", 1); 22 String url = ReadConfig.URI + ReadConfig.POST_PATH1; 23 Reporter.log("请求地址" + url); 24 JSONObject param = new JSONObject(); 25 param.put("name", doPostCase.getName()); 26 param.put("password", doPostCase.getPassword()); 27 Reporter.log("请求体" + param.toString()); 28 String result = HttpUtils.doPost(url, param.toString()); 29 Reporter.log("请求结果" + result); 30 Assert.assertEquals(result, doPostCase.getExpected()); 31 } 32 33 @Test 34 public void postTest2() throws IOException { 35 SqlSession session = DatabaseUtil.getSqlSession(); 36 DoPostCase doPostCase = session.selectOne("doPost", 2); 37 String url = ReadConfig.URI + ReadConfig.POST_PATH2; 38 Reporter.log("请求地址" + url); 39 JSONObject param = new JSONObject(); 40 param.put("name", doPostCase.getName()); 41 param.put("password", doPostCase.getPassword()); 42 Reporter.log("请求体" + param.toString()); 43 String result = HttpUtils.doPost(url, param.toString()); 44 Reporter.log("请求结果" + result); 45 Assert.assertEquals(result, doPostCase.getExpected()); 46 } 47 48 }
再次执行testng.xml, 得到的结果跟之前的效果一样. 这样设计的好处是测试用例可以用MySQL数据库管理起来
Java接口自动化测试之集成MyBatis和MySQL (五)
标签:用例 3.4 action data uri log test TE 连接数
原文地址:https://www.cnblogs.com/andrew209/p/9164112.html