Stubbing: wireMock.register(post(urlMatching(... vs. stubFor(get(urlEqualTo(...

828 views
Skip to first unread message

Christian Balzer

unread,
Oct 26, 2016, 11:26:17 AM10/26/16
to wiremock-user
Hi all,

I'm a bit confused. I'm looking at code that's about 18 months old, and I see a lot of wireMock.register(post(urlMatching(... - yet when I look at the docs (e.g. [1]), I see stubFor(get(urlEqualTo(...

How do they go together?

I know I can replace stubFor with expectThat, but that's different, right?

I want to record stubs, so my service can later call WireMock, which I want to return stubs to my service.
Also, why do I tell the client (wireMock) what the stub should be...?

Coming to think of it, I'm probably most confused why there is a WireMock client involved to begin with.

Can somebody please point me at the right places in the docs? I just can't seem to find them...

Thanks!

Kind regards,

Christian

Tom Akehurst

unread,
Oct 26, 2016, 12:22:14 PM10/26/16
to wiremock-user
stubFor() and register() have identical behaviour and they're only both present as the result of a dumb decision I made back in the early days to make the WireMock class both a container for static DSL methods and a client you could instantiate.

The reason there's a client at all is that you can run the WireMock server on another host and configure it remotely. If you're running everything in the same JVM (as you'd usually be during unit/component testing) there's no need to do this, better to call WireMockRule.stubFor() or WireMockServer.stubFor() directly.

expectThat doesn't exist in WireMock.

Hope that answers your questions. If not, perhaps you could share a little more about how you're using it?

Christian Balzer

unread,
Oct 26, 2016, 1:06:49 PM10/26/16
to wiremock-user
I think that answers my question; thanks.

I think we use the client, because we wanted to use a non-default port, as our Jenkins will build - and run tests for - several artifacts at the same time, and we had port binding issues with the default port...

One question about the example on the Getting Started page, though:
If I run WireMock in the same JVM as my service/app under test, I suppose I then have to make sure my service calls the "stubbed out system" on localhost and WireMock's port in order for it all to work?

In other words, for

Result result = myHttpServiceCallingObject.doSomething();

doSomething() would have to make an HTTP call to localhost. Right?

Tom Akehurst

unread,
Oct 26, 2016, 2:53:03 PM10/26/16
to wiremock-user
Yes, correct. You need to inject localhost and the port number into your code under test.

Binding to a random port doesn't (necessarily) mean you have to go down the client route. You can use wireMockConfig().dynamicPort() when you start it, then after it's started call wireMockServer.port() (or wireMockRule.port()).
Reply all
Reply to author
Forward
0 new messages