简体   繁体   English

在运行 junit 测试时模拟物理 Web 服务器

[英]Mock a physical web server while junit test running

I am searching for a simple framework which is able to mock JSON responses for specific URLs and therefore starts an in-memory server.我正在寻找一个简单的框架,它能够模拟特定 URL 的 JSON 响应,从而启动一个内存服务器。 Eg the framework could start a node.js server or similar.例如,框架可以启动 node.js 服务器或类似的。

I need a physical server since the unit tests are running against an external application (using selenium) and this application has a dependency to a JSON interface.我需要一个物理服务器,因为单元测试是针对外部应用程序(使用 selenium)运行的,并且该应用程序依赖于 JSON 接口。 The application itself is iPhone-App running inside of simulator and communicates to a REST interface.该应用程序本身是在模拟器内运行的 iPhone 应用程序,并与 REST 接口进行通信。

Is there an existing framework for this or what would be the best approach given that I need to execute the tests with jUnit.鉴于我需要使用 jUnit 执行测试,是否有一个现有的框架或最佳方法是什么。

You can start a stub server, given that you can configure the JSON URL in your client application.您可以启动存根服务器,前提是您可以在客户端应用程序中配置 JSON URL。

Have a look for example at:看看例如:

You can start these in your maven build during the pre-integrationtest phase and shut them down in post-integration test phase.您可以在集成前测试阶段在 Maven 构建中启动这些,并在集成后测试阶段关闭它们。

You can use npm package stubby-db .您可以使用 npm 包stubby-db What you need to do is;你需要做的是;

  1. Install : npm install stubby-db -g安装: npm install stubby-db -g
  2. Mapping : Create a request response mapping which is quite easy.映射:创建一个非常简单的请求响应映射。 Specify the path of response file having JSON response.指定具有 JSON 响应的响应文件的路径。
  3. Run the stub server: stubbydb运行存根服务器: stubbydb

That's all.就这样。 However since you just want to run it for your unit tests only which doesn't require much performance, you can also use wiremock .但是,由于您只想为不需要太多性能的单元测试运行它,因此您也可以使用wiremock It starts internally, and you never feel like you are running an external application.它从内部启动,您永远不会觉得自己在运行外部应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM