如何自动化RestAssured Web服务及解决java.net.connection连接错误?
Hey there! Let's walk through this step by step since you're just getting started with RestAssured and hitting that frustrating connection error. I'll cover both how to set up basic RestAssured automation and how to fix the connection issue you're facing.
一、RestAssured自动化基础步骤
First, let's make sure your setup is solid—manual JAR additions can miss critical dependencies, so let's start with the right foundation:
1. 优先用Maven管理依赖(比手动加JAR靠谱)
Instead of manually downloading JARs, using Maven will handle all transitive dependencies (like JSON parsers, assertion libraries) automatically. Here's a sample pom.xml snippet for RestAssured + TestNG:
<dependencies> <!-- RestAssured core --> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.4.0</version> <!-- 这个版本适配Java 8,和Eclipse Luna兼容度更高 --> <scope>test</scope> </dependency> <!-- TestNG测试框架 --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> <!-- RestAssured断言依赖的Hamcrest库 --> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> <scope>test</scope> </dependency> </dependencies>
2. 第一个RestAssured自动化测试用例
依赖配置好后,这里有个简单的TestNG测试用例,用来发送GET请求并验证响应:
import org.testng.annotations.Test; import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class RestAssuredDemoTest { @Test public void testGetRequest() { // 设置目标服务的基础URI baseURI = "http://localhost:8080"; given() .header("Content-Type", "application/json") .when() .get("/api/users") // 替换成你实际要测试的接口路径 .then() .statusCode(200) // 验证响应状态码为200 .body("size()", greaterThan(0)); // 验证响应体数据数量大于0 } }
二、解决java.net.Connection错误的常见方案
这个连接错误通常来自几个常见问题,我们逐一排查:
- 确认目标服务是否正常运行:先别着急查代码!用浏览器或Postman直接访问你测试里用的URL,如果打不开,问题出在服务本身——检查端口号、主机地址是否正确,服务有没有启动成功。
- 补充缺失的依赖:如果还是手动加JAR包,大概率漏掉了关键依赖,比如
hamcrest-all、jackson-databind或者rest-assured-json-path。换成上面的Maven配置就能自动解决依赖缺失问题。 - 配置代理(如果是公司网络):如果在公司内网,有代理服务器的话,需要给RestAssured配置代理信息:
RestAssured.proxy("你的代理主机地址", 8080); // 替换成实际的代理地址和端口 - 调整超时时间:默认的超时时间可能太短,导致服务还没响应就报错了,延长超时试试:
import io.restassured.config.HttpClientConfig; import io.restassured.config.RestAssuredConfig; RestAssured.config = RestAssuredConfig.config() .httpClient(HttpClientConfig.httpClientConfig() .setConnectionTimeout(5000) // 连接超时5秒 .setSocketTimeout(5000)); // 读取响应超时5秒 - 检查URL格式:确保URL没有拼写错误,比如漏写
http:///https://,或者结尾多了不必要的斜杠。比如http://localhost:8080/api和http://localhost:8080/api/有时候会导致连接问题。 - 适配Eclipse Luna版本:Eclipse Luna是比较老的版本(2014年发布),最新的RestAssured 5.x+需要Java 11以上,而Luna默认支持的Java版本较低。建议用RestAssured 4.x系列(比如4.4.0),它适配Java 8,和Luna兼容性更好。
三、给新手的小建议
- 从小案例开始:先测试本地的简单服务(比如Mock API或者本地启动的Spring Boot应用),再去测外部服务。
- 开启日志调试:在请求里加上
.log().all(),可以看到完整的请求和响应细节,方便定位问题:given() .log().all() .header("Content-Type", "application/json") .when() .get("/api/users") .then() .log().all();
按这个顺序逐一排查,先从最简单的服务可用性检查开始,很快就能解决问题啦!
内容的提问来源于stack exchange,提问作者Nagashri

