Home > Mobile >  Best way to mock http server in spring boot with kotlin [2022]
Best way to mock http server in spring boot with kotlin [2022]

Time:06-27

I want to test an http repository, and to do so I need to mock an HTTP server. I found this resource that goes over on one way to do so in spring boot, however, it's from 2020. I am not saying it's necessarily a bad or outdated approach, but I wanted to know if there is a more preferable or a kotlin specific way to mock HTTP server now. Any help would be appreciated.

CodePudding user response:

I would recommend using Wiremock to mock HTTP servers in your Spring Boot tests. Reasons:

  • well maintained and actively developed library (As of today: 5k stars on GitHub and last release April 29th 2022)
  • Spring boot has integration with this library (just add org.springframework.cloud:spring-cloud-contract-wiremock to your dependencies)

The team behind Spring Cloud Contract have created a library to support running WireMock using the “ambient” HTTP server. It also simplifies some aspects of configuration and eliminates some common issues that occur when running Spring Boot and WireMock together.

More info you can find here:

Spring Cloud Contract WireMock

Spring Boot Integration Tests With WireMock and JUnit 5

And here is GitHub repo with an example: spring-boot-wiremock

  • Related