本文主要是介绍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单元测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!