Hi All - I'm trying to get started with wire Mock ...
# help
r
Hi All - I'm trying to get started with wire Mock - i'm using it with Junit 4. I'm quite new to the world of Sping / Java so i'm following this guide here: https://wiremock.org/docs/quickstart/java-junit/ and i think the example code seems invalid...First up: am I right in thinking this:
Copy code
@Rule
public WireMockRule wireMockRule = new WireMockRule(8089); // No-args constructor defaults to port 8080
Should be just:
Copy code
@Rule
public WireMockRule wireMockRule;
(the documented code seems to blow up) also, in this line:
Copy code
.uri(wiremockServer.url("/my/resource"))
what's wiremockServer ? where do i instantiate that? does the @Rule not take care of that for me? also, when my code reaches
Copy code
stubFor(post("/my/resource")
It just blows up at that very point with a connection refused to http://localhost:8080 i don't quite know what i'm doing wrong - is that particular bit in the documentation correct?
l
Hi, when you say the documented code blows up, could you provide an example of the error ?
Also, is there a reason you are using junit 4? Is this an old spring project ?
r
ah the blow up when I do this: @Rule public WireMockRule wireMockRule = new WireMockRule(8089) Is an index out of bounds exception. i'm using junit 4.13.2 becuase i couldn't find v5 in our repo. however i've just this second discovered that it's actually called junit5 and is located somewhere else I canget to so i'll try with that
l
If it is a
3.x.x
spring boot project then it is probably worth looking at jUnit 5 (junit jupiter) for testing. We have just adopted a spring boot integration project wicch provides much better support - especially for Jetty 12 (which is what Spring Boot 3.x.x ships with)
Some links above to get you started
r
ace, cheers - it is indeed an SB 3 project so I'll get digging into the official SB 3 integration thanks.
l
No worries. Enjoy
r
OK junit5 is a goer but can't do the SB integration yet due to some internal rules here. So This code Here
Copy code
@WireMockTest

public class MockServerTest {
  
  @Test
  public void testMockServer(WireMockRunTimeInfo wireMockRuntimeInfo) {
    stubFor(get("/do/ping").willReturn(ok()));

  }
}
gives "indexOutOfBounds Exception"
is it correct? Cleared my IDE's cache, redownloaded all dependancies, got Junit5 v4.6.14 with wiremock 3.10.0 (i'm not exepecting it to actually do anything, btw! )
l
I haven't used spring for a while but are you sure the junit version is correct ? I thought all the junit 5 versions started with 5 ? It should be a junit jupiter version that you are using if I remember correctly
Maybe this could help. We haven't updated these examples to the new spring boot integration yet so it should be similar to what you are doing - https://github.com/wiremock/wiremock-examples/tree/main/spring-boot/spring-boot-3/wiremock-jetty-12
r
ok yep my error there - defo using junit-jupiter 5.10.5 now. still getting error index out of bounds on that small code snippet. have tried the above code with the jetty-12. It seems the
@WireMockTest
annotation throws index out of bounds exception all the time.
l
How have you added the wiremock dependency ?
r
in pom.xml
(then restarted and cleared cache)
l
Can you paste the dependency here ?
r
<dependency> <groupId>org.wiremock</groupId> <artifactId>wiremock-jetty12</artifactId> <version>3.10.0</version> </dependency>
l
Cool, so you are using the jetty 12 dependency. Could you post the stacktrace ?
r
image.png
l
That is a little strange. I have just updated the example to spring boot
3.4.1
and wiremock
3.10.0
and the tests seem to pass fine.
Is there any chance you have a small example project that you could show me so I could get a better idea what is going on. The issue you are having is because it is not finding a
HttpServerFactory
implementation. This is as a result of some changes that went into
3.10.0
but I don't know why it isn't finding an implementation.
If you can't show a small demo project then you could try changing the wiremock dependency to
3.9.2
and see if that works
I have just committed the version updates - https://github.com/wiremock/wiremock-examples
r
cool - downgrading to 3.9.2 it works.
l
That is good news, although I would still love to know what the issue with the latest version is 🙂
r
worth noting though this is spring 3.3...should have said before!
anyway thanks for your time - this is all enough to get me mocking
l
No worries. Just out of interest what is the version of spring that you are using ?
r
it is spring-core 6.1.14
👍 1
also (sorry this i think is my last question before xmas!!) can i stubfor a particular hostname call? i seem to always get 'localhost:<randomPort>' assigned but i'd like it to be 'hostNameISpecify:<randomPort>'
(and then stubFor uri's off of hostNameISpecify...)
l
Not sure I understand. Is your wiremock instance not running on
localhost
? Normally the tests just assume localhost and stub a url off of that I think. Pretty much like this example - https://github.com/wiremock/wiremock-examples/blob/main/spring-boot/spring-boot-3/[…]mockexamples/wiremockjetty12/PeopleInSpaceIntegrationTests.java
r
i got you. yeah it's just that i'm being picky. whilst they're running on localhost i was wanting a different name for it, where i could call it https://api-a but it would still end up on my localhost. no matter. Cheers foryour help.
l
That might be possible. I would need to look at the code. You can specify the bind address in the options when you configure wiremock:
Copy code
// Bind the WireMock server to this IP address locally. Defaults to the loopback adaptor.
.bindAddress("192.168.1.111")
If you are specifying a name (dns) for the base url, that would still need to be configured locally to point to 127.0.0.1. You would then run into trouble if you wanted to run the same test in a CI server or someone else on their machine who didn't have the same local config for the dns name