Ash_this
Ash_this

Reputation: 63

How to write a junit test case to test a rest client?

I have created a spring boot rest client, which basically consumes a rest service. I want to write a junit test case to validate my rest client. I was hoping to intercept the rest request somehow in the junit test case and check if the context in the URL, path parameters, query param and body is correct. i just want to confirm, if anyone uses the rest client, it will make the proper calls to the rest services.

Upvotes: 1

Views: 3206

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42431

Here are some approaches you can try:

  1. Use @RestClientTest annotation. It will load only the class that contains rest client calls and also configures the application context during the test to provide ways to match the request for verifications.

You can read this article for example

  1. Use WireMock - you can start it at during the setup phase setup the expectations, during the test make the calls and at the end verify that the calls have matched the expected ones. When you make the request to wire mock it will "record" it and store in memory (until you reset it). It also starts on the real port so you should have a port available. For more information read this chapter of the official documentation

Upvotes: 2

Related Questions