Write an unit test without mocking the rest template

How would you call a JUnit test that calls an external service ?
-> An integration test, why not…

How about a JUnit test that replaces the implementation of the repository layer by a mock ?
-> An unit test, fine.

Would you appreciate to write an unit test that simulates the integration without the pain of recording the response by yourself ?
With a very small boilerplate code, you can do a DIY© implementation of that.

No additional framework, no surprise.

This program will save the mocks inside target/test-classes/rest-template-mocks.
Once satisfied, copy them all inside src/test/resources/rest-template-mocks

There you are, the tests will always be using the mocks. And always green (maybe).

It is written in Kotlin but you can get the Java version in no time

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.