Skip to content
Advertisement

How to use WireMock’s response template in JUnit 5 Tests

I’m trying to use the Response Templating feature of WireMock, but it does not seem to work with the sample piece of code provided in the docs.

This is a sample piece of code:

JavaScript

Expected Output:

Tests should pass. (meaning the {{request.url}} should be substituted with /test-url as a result of template rendering).

Actual Output:

JavaScript

Things I’ve tried:

  1. Since these are test cases using JUnit 5 API, did not add @Rule WireMockRule, instead added the .withTransformers("response-template").
  2. Tried changing the test cases to use JUnit 4 API, and added
JavaScript

(along with withTransformers)
3. Changed the WireMockRule to

JavaScript

(along with withTransformers)
4. Removed the withTransformers only keeping the WireMockRule. (JUnit 4)
5. I’ve also tried the above combination with JUnit 5 API too.

But none of the above variations worked. Is there anything that I’m missing?

Advertisement

Answer

The @Rule approach won’t work because you are ignoring the WireMockServer created/managed by te rule as you are creating a new one yourself in the @BeforeEach.

You should remove the rule and add the ResponseTemplateTransformer in your @BeforeEach to the WireMockServer through the Options object.

Something like this should do the trick (judging from the Javadoc).

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement