testNg单元测试
2021/5/21 18:27:35
本文主要是介绍testNg单元测试,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import cn.com.maiwe.maxview.utils.UserUtil; import cn.com.maiwe.maxview.vo.UserLoginVO; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.digest.BCrypt; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.web.bind.annotation.RequestParam; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import static com.sun.org.apache.xml.internal.serialize.OutputFormat.Defaults.Encoding; //@Slf4j @SpringBootTest public class MaxviewTest { CloseableHttpClient httpClient = HttpClients.createDefault(); @Test(dataProvider = "datas",timeOut = 3000) public void a( String phone/*, String password, String finger*/) { String restUrl = "http://127.0.0.1:8001/user/user/login"; //1.创建post对象,以post方式提交接口请求 HttpPost httpPost = new HttpPost(restUrl); httpPost.setHeader("Content-Type","application/json"); //2.准备提交参数 // List<NameValuePair> params = new ArrayList<>(); // BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("phone", phone); // BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("password", password); // BasicNameValuePair basicNameValuePair3 = new BasicNameValuePair("finger", finger); // params.add(basicNameValuePair1); // params.add(basicNameValuePair2); // params.add(basicNameValuePair3); // UserLoginVO login = new UserLoginVO(); // login.setPhone(phone); // login.setPassword(password); // login.setFinger(finger); // String jsonString = JSON.toJSONString(login); // System.out.println(jsonString); //3.参数封装到请求体中 try { // httpPost.setEntity(new UrlEncodedFormEntity(params)); httpPost.setEntity(new StringEntity(phone,Encoding)); System.out.println("method:" + httpPost.getMethod()); //4.准备客户端 //5.提交请求 CloseableHttpResponse httpResponse = httpClient.execute(httpPost); //6.解析接口返回数据,返回字符串 String result = EntityUtils.toString(httpResponse.getEntity()); //7.输出结果到控制台验证 System.out.println("*****返回数据:" + result); } catch (Exception e) { e.printStackTrace(); } } @DataProvider public Object[][] datas() { Object[][] datas = {{"{\"finger\":\"asdasd\",\"password\":\"8VCr9S90PhMfT2S8zA0Gug==\",\"phone\":\"8VCr9S90PhMfT2S8zA0Gug==\"}"} // {"123456","654321","asdasd"}, // {"8VCr9S90PhMfT2S8zA0Gug==","8VCr9S90PhMfT2S8zA0Gug==","asdasd"} }; return datas; } }
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!-- definition of appender STDOUT --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern> </encoder> </appender> <root level="ERROR"> <!-- appender referenced after it is defined --> <appender-ref ref="STDOUT"/> </root> </configuration>
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.3</version> <scope>test</scope> </dependency>
这篇关于testNg单元测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南