Rest Assured+Serenity抽取环境配置类时出现空指针问题求助
Serenity+Rest Assured API自动化:解决空指针异常与代码复用优化
空指针异常的根因及修复
你遇到的空指针异常是因为EnvironmentVariables未被Serenity正确注入。只需给该字段添加@Inject注解,让Serenity自动注入实例即可:
// 替换原有的private EnvironmentVariables声明 @Inject private EnvironmentVariables environmentVariables;
代码复用的具体优化方案
1. 抽取公共配置与工具方法
把重复的BaseURI获取、Rest Assured请求模板抽成私有方法,避免每个步骤重复编写:
// 获取BaseURI的公共方法 private String getBaseUri() { return EnvironmentSpecificConfiguration.from(environmentVariables) .getProperty("base.api.url"); } // 公共请求模板,包含通用的header和contentType private RequestSpecification getCommonRequest() { return SerenityRest.given() .contentType("application/json") .header("Content-Type", "application/json"); }
2. 封装通用搜索方法
将三个搜索步骤合并为一个通用方法,通过参数传递可变的查询条件(日期、天数、ID),同时处理可选参数:
// 通用搜索方法,覆盖所有场景 @Step("执行GET搜索: 日期 {date}, 天数 {days}, ID {id}") public void performSearch(LocalDate date, Integer days, Integer id) { RequestSpecification request = getCommonRequest() .queryParam("type", 2) .queryParam("date_req", date.toString()); // 仅当参数不为空时添加查询条件 if (days != null) request.queryParam("days", days); if (id != null) request.queryParam("id", id); response = request.get(getBaseUri() + basePath); System.out.println("Search json response:::==>" + response.prettyPrint()); } // 原有的三个步骤方法简化为对通用方法的调用 @Step("执行当天GET搜索请求") public void searchCurrentDay() { performSearch(today, 1, 1); } @Step("执行明日GET搜索请求") public void searchTomorrow() { performSearch(tomorrow, null, null); } @Step("执行后天GET搜索请求") public void searchNextDay() { performSearch(nextday, null, null); }
3. 全局配置简化(可选)
在serenity.conf中配置Rest Assured全局参数,省去每个请求重复设置contentType的步骤:
restassured { config = { content-type = "application/json" } }
配置后,getCommonRequest()方法可以进一步简化,甚至无需单独抽取。
完整优化后的代码示例
import net.serenitybdd.core.environment.EnvironmentSpecificConfiguration; import net.serenitybdd.rest.SerenityRest; import net.serenitybdd.core.annotations.Step; import javax.inject.Inject; import java.time.LocalDate; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; public class BookingSearchSteps { LocalDate today = LocalDate.now(); LocalDate tomorrow = today.plusDays(1); LocalDate nextday = today.plusDays(2); @Inject private EnvironmentVariables environmentVariables; public Response response; String basePath = "/booking/search"; private String getBaseUri() { return EnvironmentSpecificConfiguration.from(environmentVariables) .getProperty("base.api.url"); } private RequestSpecification getCommonRequest() { return SerenityRest.given() .header("Content-Type", "application/json"); } @Step("执行GET搜索: 日期 {date}, 天数 {days}, ID {id}") public void performSearch(LocalDate date, Integer days, Integer id) { RequestSpecification request = getCommonRequest() .queryParam("type", 2) .queryParam("date_req", date.toString()); if (days != null) request.queryParam("days", days); if (id != null) request.queryParam("id", id); response = request.get(getBaseUri() + basePath); System.out.println("Search json response:::==>" + response.prettyPrint()); } @Step("执行当天GET搜索请求") public void searchCurrentDay() { performSearch(today, 1, 1); } @Step("执行明日GET搜索请求") public void searchTomorrow() { performSearch(tomorrow, null, null); } @Step("执行后天GET搜索请求") public void searchNextDay() { performSearch(nextday, null, null); } }
内容的提问来源于stack exchange,提问作者fareen taj
相关产品推荐
相关产品推荐

