- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以帮我将下面的代码重构为 Spring RestTemplate 吗? postLogin
是稍后在 junit e2e 测试中使用的方法。
public class LoginLogoutAPI {
private static final LoginLogoutAPI INSTANCE = new LoginLogoutAPI();
private static final String LOGIN_ENDPOINT = "/auth/login";
public static LoginLogoutAPI getInstance() {
return INSTANCE;
}
public ValidatableResponse postLogin(String login, String password) {
return given()
.contentType(JSON)
.body(getCustomerCredentialsJson(login, password))
.when()
.post(LOGIN_ENDPOINT)
.then()
.statusCode(SC_OK);
}
private Map<String, String> getCustomerCredentialsJson(String login, String password) {
Map<String, String> customer = new LinkedHashMap<>();
customer.put("login", login);
customer.put("password", password);
return customer;
}
}
最佳答案
假设您的所有内容都正确,我将实现 Rest Template Exchange
方法进行发布调用并捕获 ValidatableResponse
中的响应.
public class LoginLogoutAPI {
private static final LoginLogoutAPI INSTANCE = new LoginLogoutAPI();
private static final String LOGIN_ENDPOINT = "/auth/login";
public static LoginLogoutAPI getInstance() {
return INSTANCE;
}
public ValidatableResponse postLogin(String login, String password) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<byte[]> httpEntity = new HttpEntity<byte[]>(headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(LOGIN_ENDPOINT)
.queryParam("login",login)
.queryParam("password",password);
URI uri=builder.buildAndExpand().toUri();
ResponseEntity<ValidatableResponse> rs = restTemplate.exchange(uri, HttpMethod.POST, httpEntity,ValidatableResponse.class);
return rs.getBody();
}
}
这是一个实现,但不是一个工作示例,因为我没有工作区设置。您必须更换您的 LOGIN_ENDPOINT
以及其余模板的完整 URL。
如果您需要澄清,请告诉我!
关于java - 将 RestAssured 转换为 RestTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42716689/
我正在尝试编写一个用于 API 测试的 BDD 框架。导入静态 io.restassured.RestAssured.*;不工作。另外,当我使用给定()关键字时它会抛出错误。我已经添加了各自的 Mav
您好,我在使用放心 4.1.1 时无法解决该错误。我的 Eclipse IDE 中的库。我已经在我的 pom.xml 文件中添加了放心库,但错误仍未解决。 我尝试从 https://mvnreposi
运行测试时出现以下错误。我正在尝试将 API 响应打印到文件,但是测试失败并引发错误。调用的响应采用 JSON 格式,采用 GZIP 格式。任何想法和想法都将不胜感激。 错误: io.restassu
我正在尝试使用 Java 的 RestAssured API 进行 OKTA 身份验证。有问题的身份验证类型是多因素的,虽然我总是能够在 Google Chrome 中成功进行此身份验证,但在 Res
我正在尝试设置最新版本的 spring cloud contract。但是总是抛异常,无法初始化。我的毕业典礼看起来像这样。我可以在外部库中看到,2.9.0 是可见的。 dependencies {
前言 不知道大家的项目是否都有对接口API进行自动化测试,反正像我们这种小公司是没有的。由于最近一直被吐槽项目质量糟糕,只能研发自己看看有什么接口测试方案。那么在本文中,我将探索如何使用 R
我有 RESTAssured 的测试(!)代码,它检查 REST 端点是否返回 0 作为状态代码; given() .contentType(CONTENT_TYPE_APP
stackoverflowians:) 我已经停止使用 Gmail API 进行 Post 调用了。一直在尝试使用createDraft和createLabel。现在我想我已经找到了如何正确执行此操作
我正在尝试使用 RestAPI 编程获取多个 URI。所有的GET请求都需要基本授权 我使用了一个基类,在其中定义了 RequestSpecification,如下所示(以避免每次运行测试时都使用授权
假设我有以下模型: @Data class Model { private String someField; private String otherField; } 以及 Rest
我在测试中通过的对象 @Data public class UserRequest { @JsonProperty("name") private final String name; @JsonPr
在rest-assured中使用requestSpecBuilder,我在日志文件中获取日志两次。如何禁用此功能? @BeforeClass public void initSpec() {
(注意:我已经看到多个关于“Jackson 提示无法识别的属性”主题的问题得到了回答,但我没有找到专门使用 RestAssured 的对象映射器的问题) 我有一个 RestAssured 测试,它有效
我正在尝试配置并发送如下所示的多部分请求: ------boundary Content-Disposition: form-data; name="before"; filename="blob"
我有一些使用 RestAssured 的 Java 测试。对于许多测试,given() 和when() 参数不同,但then() 部分是相同的并且由多个assertThat() 语句组成。如何将 th
我正在使用放心为我的微服务编写集成测试。我有一个像这样的 Json 有效负载,它是从它返回的。 { "sessionQuestions":[ { "id":1272
有人可以让我了解如何验证响应中的项目列表吗?假设响应如下所示, { "store":{ "book":[ { "author":"N
我们有 REST 网络服务。它对 JSON 数据表示进行操作。我想提供功能测试。我打算使用 RestAssured framework .它提供了可理解的方法来测试输出 json 的正确性。 示例,g
我想默认为所有 RestAssured 响应和请求启用日志记录。 这是我的做法: RestAssured.requestSpecification = new RequestSpecBuilder()
有没有办法将整个JSON路径数据转换为Java中的字符串? 我正在处理 API,它们的响应采用 JSON 格式。通过 Postman/WireShark 很容易理解 JSON 结构,但我试图通过 Ja
我是一名优秀的程序员,十分优秀!