简体   繁体   English

使用 SpringBoot 配置文件模拟 REST API 调用

[英]Mocking REST API calls with SpringBoot profiles

I have recently started out with Spring and am unsure about how to approach this issue.我最近开始使用 Spring,但不确定如何解决这个问题。 I have a Spring boot program which makes calls to remote REST APIs.我有一个调用远程 REST API 的 Spring 启动程序。 For example an AddressService class with getAddress(String user) method, which makes a HTTP call and returns a JSON response.例如带有getAddress(String user)方法的AddressService类,它进行 HTTP 调用并返回 JSON 响应。 I would like to set up Spring profiles for development purposes local , dev , uat , prod .我想为localdevuatprod开发目的设置 Spring 配置文件。

When the program is running with the local profile, I would like to "mock" these external API calls with an expected JSON response, so I can just test logic, but when it is run in any of the other profiles I would like to make the actual calls.当程序使用local配置文件运行时,我想用预期的 JSON 响应“模拟”这些外部 API 调用,因此我可以只测试逻辑,但是当它在我想做的任何其他配置文件中运行时实际调用。 How can I go about doing this?我该怎么做呢? From what I read, there's many ways people approach this, using WireMock , RestTemplate , Mockito etc. I'm confused about which is the way to go.从我读到的内容来看,人们有很多方法来解决这个问题,使用WireMockRestTemplateMockito等。我对要走的路感到困惑。

Any advice would be greatly appreciated.任何建议将不胜感激。 Thanks.谢谢。

WireMock,Mockit is for unittest, to mock the real request. WireMock,Mockit 用于单元测试,模拟真实请求。 Example here: How do I mock a REST template exchange?此处示例: 如何模拟 REST 模板交换?

When you need a running implementation with a mock, i think the easiest way is that you have a interface当你需要一个带有模拟的运行实现时,我认为最简单的方法是你有一个接口

public interface AdressAdapter {
    public List<Adress> getAddress(String name);
}

And two different implementations depending on the profile.以及两种不同的实现,具体取决于配置文件。

@Profile("local")
public class DummyAdress implements AdressAdapter{
    @Override
    public List<Adress> getAddress(String name) {
        //Mock here something
        return null;
    }
}

! means NOT locale profile in this case.在这种情况下意味着不是语言环境配置文件。

@Profile("!local")
public class RealAdress implements AdressAdapter{
    @Override
    public List<Adress> getAddress(String name) {
        //Make Restcall
        return null;
    }
}

What you could do is use different application.properties files depending on your profile.您可以做的是根据您的个人资料使用不同的application.properties文件。 That way, you just change the url to a mock server for your local profile.这样,您只需将 url 更改为local配置文件的模拟服务器。

So what you have to do is :所以你要做的是:

  1. Create another application.properties in your resources folder named : application-local.properties .在名为application-local.properties resources文件夹中创建另一个application-local.properties
  2. Change the url of the desired service.更改所需服务的 url。
  3. Start your application with the VM option -Dspring.profiles.active=local .使用 VM 选项-Dspring.profiles.active=local启动您的应用程序。

Here is a link that describe well what you want to achieve.这是一个链接,可以很好地描述您想要实现的目标。

For your mock server, you could use Wiremock , Mountebank , Postman ,... that can be start separately and mock specific endpoints to return what you want.对于您的模拟服务器,您可以使用WiremockMountebankPostman 等,它们可以单独启动并模拟特定端点以返回您想要的内容。

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

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