错误的代码:
package pro.data;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import pro.assertion.MyAssert;
//sb124--jenkis
public class AddFund {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
MyAssert assertion = new MyAssert();
private int i = 5;
@Parameters({ "myNumber" })
public AddFund(@Optional("5") int i) {
this.i = i;
}
@BeforeClass
public void beforeClass() {
client = HttpClients.createDefault();
System.out.println("beforeClass-hc");
}
@Test(testName = "login")
public void login() {
System.out.println("i 先登录");
}
@Parameters({ "myNumber" })
@Test(dependsOnMethods = {"login" }, groups = { "ga" })
public void add(String userId, String fundCode, String fundName, String holdPortion) {
System.out.println("执行第" + i + "次");
String content = "";
HttpGet request = new HttpGet("http://localhost:9999");
try {
// 此处可以解决中文乱码
fundName = URLEncoder.encode(fundName, "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String path = "/addFundRpWithParams?userId=" + userId + "&fundCode=" + fundCode + "&fundName=" + fundName
+ "&holdPortion=" + holdPortion;
request.setPath(path);
try {
System.out.println(request.getRequestUri());
response = client.execute(request);
HttpEntity entity = response.getEntity();
content = EntityUtils.toString(entity, "utf-8");
// System.out.println(content);
} catch (Exception e) {
e.printStackTrace();
}
assertion.assertEquals(content.contains("成功"), true);
assertion.assertAll();
}
@Test(groups= {"gb"})
public void pr() {
System.out.println("当前线程id:"+Thread.currentThread().getId());
}
}
<test name="add-fund" group-by-instances="true">
<packages>
<package name="pro.data" />
</packages>
<groups>
<run>
<include name="ga" />
</run>
</groups>
</test>
可以看出方法add所在的组为“ga",并且依赖方法login,不过login并未处在任何分组中,通过xml执行的时候,login方法不在执行范围,所以导致org.testng.TestNGException--depends on nonexistent method "pro.data.AddFund.login" 错误
解决办法:
只要将依赖的放也放进被执行的组中即可
@Test(testName = "login",groups = { "ga" })
public void login() {
System.out.println("i 先登录");
}