在空手道API中,模拟无法按照我的预期进行 [英] In Karate API mocking not working as expected for me

查看:73
本文介绍了在空手道API中,模拟无法按照我的预期进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索空手道API双(模拟)以进行集成测试.对于以下情况,我没有得到预期的模拟响应.您的帮助将不胜感激.

I am exploring Karate API double (mocking) for the integration test. For the below scenarios, I'm not getting the expected mocking response. Your help will be appreciated.

我的设置: 1.空手道模拟服务器,在端口 8001 上使用pathMatches规则: http://localhost:8001 (工作正常,已针对"/cat"和一些测试调用进行了验证) 2.我自己的应用程序是从docker在端口 8080 上启动的.从Docker也暴露了8001端口.

My Setup : 1. Karate Mock Server up with pathMatches rules on port 8001: http://localhost:8001 ( working, validated against "/cat" and some test calls) 2. My own Application is up from docker on port 8080. From Docker exposed 8001 port as well.

模拟案例: 1.向所有用户公开的 应用REST调用 http://localhost:8080/service/v1/findUser .此公开的API,底层,调用其他 REST调用 http ://dev-STG/userservice/v1/findUser 实际上提供了JSON响应.因此,我想模拟底层API调用并相应地验证我的API行为.

Mocking Case: 1. My application REST call exposed to all users http://localhost:8080/service/v1/findUser. This exposed API, underlying calling other REST call http://dev-STG/userservice/v1/findUser which actually giving JSON response. So, I want to mock underlying API call and validate my API behavior accordingly.

尝试的步骤: 1.现在,在我的应用程序配置中,m替换对空手道模拟服务器的实际基础API调用( http://本地主机:8001/userservice/v1/findUser ).然后确实建立了我的应用程序泊坞窗.

Steps tried: 1. Now, in my application config, m replacing actual underlying API call to Karate mock server(http://localhost:8001/userservice/v1/findUser). Then did build & up my application docker.

  1. 在空手道中,我定义了测试,例如"testIntgrtn.feature",它调用我的应用程序API "
  2. 执行"testIntgrtn.feature"空手道后,不对基础调用进行模拟( http://localhost: 8001/userservice/v1/findUser ).
  3. 现在,在"testIntgrtn.feature"文件中,我将我的应用程序URL更改为基础的REST URL,即(

  1. In Karate, I defined test e.g "testIntgrtn.feature" which calling my application API "http://localhost:8080/service/v1/findUser" and Karate mock server up and set with pathmatch "/userservice/v1/findUser".
  2. After executing "testIntgrtn.feature" karate not mocking for an underlying call(http://localhost:8001/userservice/v1/findUser).
  3. Now, in "testIntgrtn.feature" file I changed my-application URL to underlying REST URL i.e (http://localhost:8001/userservice/v1/findUser) then mocking will work like charm.

我不明白为什么底层API调用在这里没有被嘲笑?我在这里想念什么吗? 此外,在空手道中,我们可以监视所有REST调用(如cypress模拟).

I'm not understanding why underlying API call not getting mocked here? Did I miss something here? Also, in Karate can we monitor all REST calls (like cypress mocking).

感谢这个出色的框架.这对于编写自动化案例很直观.

Thanks for this wonderful framework. Which is intuitive for writing automation cases.

推荐答案

空手道无法自动拦截呼叫.

Karate cannot automatically intercept calls.

推荐的方法是,当启动在localhost:8080上运行的应用程序时,更改配置,以使它调用http://localhost:8001/v1/findUser之类的东西而不是调用http://dev-stg/userservice/v1/findUser.这是大多数团队所做的事情,并且很容易,因为您应该将外部URL定义为application.properties(或等效名称)作为最佳实践.

The recommended approach is when you boot the application running at localhost:8080 you change the configuration so that instead of calling http://dev-stg/userservice/v1/findUser it calls something like http://localhost:8001/v1/findUser. This is what most teams do, and is easy because you should anyway be defining external URL-s as application.properties (or equivalent) as a best-practice.

例如,在Spring Boot中覆盖应用程序属性非常容易,您可以通过命令行执行此操作: https://stackoverflow.com/a/37053004/143475

It is very easy to over-ride an application property in Spring Boot for example, you can do this via the command-line: https://stackoverflow.com/a/37053004/143475

如果需要,可以动态地提供模拟的端口.因此,您的单元测试可以首先启动模拟,获取端口,然后启动服务器.您可以在空手道文档中找到详细信息.

If you want, you can dynamically provision a port for the mock. So your unit test can first start a mock, get the port, and then start the server. You can find details in the Karate documentation.

所有这些说明,如果您能够在localhost:8080处的应用程序启动之前更改(系统)HTTP代理,则可以在不修改配置的情况下执行此操作. (但是这很棘手,所以我建议使用上面介绍的方法.)因此,在这种情况下,空手道实际上可以拦截"应用程序在localhost:8080处发出的HTTP调用.

All this said, if you are able to change the (system) HTTP proxy before the app at localhost:8080 starts, you may be able to do this without modifying the configuration. (But it is tricky, so I recommend the approach explained above.) So in this case, Karate can actually "intercept" the outgoing HTTP calls that the app at localhost:8080 makes.

在此处查看表中的倒数第二行(5a): https://github.com/intuit/karate/tree/master/karate-netty#consumer-provider-example

See the second-last row (5a) in the table here: https://github.com/intuit/karate/tree/master/karate-netty#consumer-provider-example

这篇关于在空手道API中,模拟无法按照我的预期进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆