- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
api调用的Java代码
我想知道如何测试下面两行代码。
private void api(){
//Code to call an API and i want to test this in groovy spock
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
RestTemplate restTemplate = new RestTemplate();
String url ="url";
String body ="body";
//How to mock below line
RequestEntity<String> requestEntity = RequestEntity.put(new URI(url)).contentType(MediaType.APPLICATION_JSON).body(body);
//And this line
ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity,String.class);
HttpStatus StatusCode = responseEntity.getStatusCode();
}
最佳答案
这不是一个 Spock 问题(您没有提供一行 Spock 规范,所以到目前为止没有人知道您尝试了什么),而是一个软件工程或一般测试问题。测试意大利面条代码的问题——这里我指的是既做某事又同时创建大量对象的代码——是从外部无法访问在方法内创建并存储在局部变量中的对象。有两种方法可以重构代码以获得更好的可测试性:
如果有意义,请更改代码以使用户能够从外部注入(inject)依赖项,而不是代码在内部创建它们。请谷歌“依赖注入(inject)”并找到像
这样的变体另一种方法是将方法的对象创建部分分解为较小的生产者(或创建者或工厂)方法,然后您可以在使用部分模拟时根据您在测试中的选择覆盖( stub ) ( spy )对象。 Spock 提供了这样的 spy ,因此您可以轻松使用它们。
我将展示后一种方法供您引用:
package de.scrum_master.stackoverflow.q58101434;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
public class MyRestApi {
public HttpStatus api() throws URISyntaxException {
//Code to call an API and i want to test this in groovy spock
HttpHeaders httpHeaders = createHttpHeaders();
RestTemplate restTemplate = createRestTemplate();
String url ="url";
String body ="body";
//How to mock below line
RequestEntity<String> requestEntity = createRequestEntity(url, body);
//And this line
ResponseEntity<String> responseEntity = executeRequest(restTemplate, requestEntity);
return responseEntity.getStatusCode();
}
HttpHeaders createHttpHeaders() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return httpHeaders;
}
RestTemplate createRestTemplate() {
return new RestTemplate();
}
RequestEntity<String> createRequestEntity(String url, String body) throws URISyntaxException {
return RequestEntity.put(new URI(url)).contentType(MediaType.APPLICATION_JSON).body(body);
}
ResponseEntity<String> executeRequest(RestTemplate restTemplate, RequestEntity<String> requestEntity) {
return restTemplate.exchange(requestEntity,String.class);
}
}
看看该方法现在如何更加结构化和更具可读性(仍然可以改进,我只是以一种快速而肮脏的方式做到了)?请特别注意辅助方法 createRequestEntity
和 executeRequest
。
下面是编写 Spock 测试的方法:
package de.scrum_master.stackoverflow.q58101434
import org.springframework.http.HttpStatus
import org.springframework.http.RequestEntity
import org.springframework.http.ResponseEntity
import spock.lang.Specification
import spock.lang.Unroll
class MyRestApiTest extends Specification {
@Unroll
def "API returns status code #statusCode"() {
given: "prepare mocks + spy"
RequestEntity<String> requestEntity = Mock()
ResponseEntity<String> responseEntity = Mock() {
getStatusCode() >> httpStatus
}
MyRestApi myRestApi = Spy() {
createRequestEntity(_, _) >> requestEntity
executeRequest(_, _) >> responseEntity
}
when: "execute API method"
def result = myRestApi.api()
then: "check expected results"
// This actually only tests mockfunctionality, your real test would look differently
statusCode == result.value()
reasonPhrase == result.reasonPhrase
where:
httpStatus | statusCode | reasonPhrase
HttpStatus.OK | 200 | "OK"
HttpStatus.MOVED_PERMANENTLY | 301 | "Moved Permanently"
HttpStatus.UNAUTHORIZED | 401 | "Unauthorized"
}
}
如果您不理解这段代码,请随时提出(相关!)后续问题。我建议您学习更多关于整洁代码、可测试性、一般模拟测试以及特别是 Spock 的知识。
关于java - 如何在 Spock groovy 中模拟 RequestEntity.put() 和 restTemplate.exchange(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58101434/
有没有办法使用 EWS Managed API 搜索 Exchange 以查找所有文件夹中的所有电子邮件。我正在使用 FindItems API 调用——但这似乎要求将搜索限制在单个文件夹中。
我已经看到这个问题被问了好几次,但到目前为止,答案都非常机械化和令人失望: EWS 与 EAS 有什么区别? 现在,大多数网站都给出以下内容:“一个是移动设备协议(protocol),另一个是 Web
我们将 Exchange 集成到我们的 CRM(日记和电子邮件)中;我们通过 SQL 2008 服务器中的 CRL 函数完成了这项工作,这些函数处理所有身份验证以及与 Exchange 2007 We
我想为交换服务器创建插件。 Exchange 服务器是否为此提供任何 SDK。如果有人知道,请告诉我。 最佳答案 此链接应为您提供可用内容的概览。 https://msdn.microsoft.com
我刚开始使用EWS(Exchange Web服务),并且正在寻找一个简单的示例,该示例演示如何发送带有附件的电子邮件。我搜索了一个示例,但找不到简单明了的示例。我找到了有关如何发送电子邮件但不发送带有
我在一个客户站点工作,并通过网络邮件连接到他们的 Exchange 2000 服务器。我正在运行 Outlook 2007,并通过 HTTP 连接到我的公司 Exchange 2007。 是否有任何方
从 Exchange 2007 开始,安装 Exchange 时不再安装 MAPI。在这些系统上 this install如果您想使用 MAPI,则除 Exchange 外还需要。 我的软件依赖于 E
交换 2010。 我已经阅读了有关 Exchange 中的通知订阅、邮箱事件和 EWS 的信息,据我所知,通知客户端应用程序必须始终与 EWS 保持连接。我对吗? 我的任务看起来很简单 - 当消息到达
我试图弄清楚节流政策如何影响 EWS。 对于 EWS,我们有以下值: EWSMaxSubscription:模拟用户完成的事件订阅数。 EWSMaxConcurrency:单个客户端可能执行的并发连接
我在 Exchange 2010 SP2 服务器上使用 EWS 我似乎找不到任何命令/文档来检索交换服务器中所有用户(邮箱/别名)的完整列表 这个问题已经问了好几次了,但我还没有看到任何答案 谢谢 最
我正在使用 CalendarItemType View 来检索日历项目。我唯一关心的项目是我创建的项目,我知道它们都是每周重复出现的项目。我能够获取每个单独的事件,并从其中任何一个中获取重复出现的主项
我从 Exchange 2007 服务器的地址开始: user@domain.exchangeserver.org 我尝试发送自动发现请求,如 MSDN 中所述. 我尝试使用 TechNet Whit
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 12 年前。 Improve thi
这是我所拥有的... 我有一个跟踪条形码类型标签的程序。我可以在数据库中选择一个项目并为它打印一个标签。我正在添加向我们的 Exchange 服务器 (2007 SP1) 上的特定收件箱发送电子邮件并
我的公司正在开发一个桌面和移动电子邮件客户端的项目,该客户端可以连接到不同的邮件服务器,用户或服务器管理员只需进行最少的配置。我们希望能够支持 Exchange,但经过一些研究后我意识到有许多问题需要
我正在尝试调用 GetUserAvailabilityRequest 来自不遵守夏令时的南非标准时间,但是,TimeZone 元素需要 StandardTime 和 DaylightTime 子元素,
我想在我的交换服务器上收到电子邮件时运行脚本。我真的没有关于如何实现这一点的好方向。我已经用 VB 编写了脚本并在 outlook 上对其进行了测试。我错误地认为您可以像在 Outlook 中一样从
我在使用 Exchange 2010 Web 服务 (EWS) 创建全天事件约会时遇到了麻烦。 根据现有要求,创建全天事件约会对象需要指定开始和结束时间(即 10/20/2011 12:00:00 A
是否有任何公共(public) Exchange 服务器(模拟器)可用于测试/试验我的 Exchange Web 服务实现? 最佳答案 我怀疑没有免费的东西。创建自己的 VM 会容易得多。您将需要具有
我正在尝试使用最新的 Exchange Web 服务 DLL,但是当我搜索我的硬盘时,我看到了许多版本的 Microsoft.Exchange.WebServices.DLL,最新的是版本 14.0.
我是一名优秀的程序员,十分优秀!