2

Wire mock has a addMockServiceRequestListener function available on the JUnit4 Rule or on a wiremock server instance.

How do I get to that function from a test class annotated with JUnit 5's @WireMockTest annotation?

More generally, how do I get an instance of the WireMockServer from a test in a class that uses @WireMockTest ?

Gonen I
  • 5,576
  • 1
  • 29
  • 60

2 Answers2

1

Would retrieving the DSL from WireMockRunTimeInfo sufficient in your case?

https://wiremock.org/docs/junit-jupiter/

https://javadoc.io/doc/com.github.tomakehurst/wiremock-jre8/latest/com/github/tomakehurst/wiremock/junit5/WireMockRuntimeInfo.html#getWireMock--

From WireMockRunTimeInfo , there is a getWireMock() method which returns WireMock.

Example:

@WireMockTest
public class DeclarativeWireMockTest {

    @Test
    void test_something_with_wiremock(WireMockRuntimeInfo wmRuntimeInfo) {
        // The static DSL will be automatically configured for you
        stubFor(get("/static-dsl").willReturn(ok()));
      
        // Instance DSL can be obtained from the runtime info parameter
        WireMock wireMock = wmRuntimeInfo.getWireMock();
    
        wireMock.register(get("/instance-dsl").willReturn(ok()));
       
        // Info such as port numbers is also available
        int port = wmRuntimeInfo.getHttpPort();
        
        // Do some testing...
    }
}
JCompetence
  • 6,997
  • 3
  • 19
  • 26
1

There's a better option in newer versions of WireMock than calling addMockServiceRequestListener, which is to register a PostServeAction implementation as an extension when configuring JUnit:

@RegisterExtension
static WireMockExtension wm =
    WireMockExtension.newInstance()
          .options(wireMockConfig().dynamicPort()
              .extensions(new PostServeAction() {
                @Override
                public String getName() {
                  return "my-action";
                }

                @Override
                public void doGlobalAction(ServeEvent serveEvent, Admin admin) {
                  // Do something
                }
              }))
          .build();

PostServeAction implementations are the "proper" way to listen for events and will still work in future versions, whereas listeners will be deprecated and removed eventually. They also are given more context about the request than listeners.

Tom
  • 3,471
  • 21
  • 14
  • Thanks for pointing this out. Currently I am unable to use it since it seems to start a new server instance, and I have to use a pre-existing wiremock server. Is there any way to register the extension to the existing server? – Gonen I Mar 07 '22 at 11:25
  • Maybe better or not, but is there really no way to get the WireMockServer? Cause simply, we have code built around it, without we would need to rewrite quite a lot. – ayan ahmedov Oct 30 '22 at 14:38
  • @ayanahmedov What do you need from WireMockServer that you can't get from the runtime info object or WireMock (the client)? – Tom Oct 31 '22 at 15:32
  • @tom it is about searching for a quick way get things compiled. – ayan ahmedov Nov 04 '22 at 14:46