https://linen.dev logo
#general
Title
# general
k

Kushagra Kapoor

06/13/2023, 11:40 AM
Hello, Can someone please help me with the dynamic port usage? I wrote a wiremock test using @WiremockTest annotation. Wiremock server is coming up on a default port. I am able to call my service from the test but I am not able to understand how do I modify the wiremock's dynamic port on the fly in the service, so that service makes the third party service call to wiremock instead of actual third party service. I am blocked on this. Request the team to help. P.S. - Intent is to run each wiremock test class with a separate wiremock server on dynamic port against the service.
t

Tom

06/13/2023, 11:55 AM
You can get the port WireMock is running on by calling
wmRuntimeInfo.getHttpPort()
I suggest using this rather than hard-coding to e.g. 8080
k

Kushagra Kapoor

06/13/2023, 11:57 AM
@Tom 8080 here is not the wiremock port but the port where my service is running. Flow is like: Wiremock -> My service -> Third party service Wiremock server is coming up on the dynamic port but how do I make this dynamic port configured in "My service" so that the call which was intended to go to "Third party service", now goes to wiremock server
In one of the post I saw the usage of @DynamicPropertySource to override the remote url from test. But this annotation is specific to spring, is this the only way to do it or does wiremock provides out of the box support for this? Ref: 1. https://wiremock-community.slack.com/archives/C03N1E6HFPY/p1683009721602629 2. https://rieckpil.de/spring-boot-integration-tests-with-wiremock-and-junit-5/
t

Tom

06/13/2023, 12:09 PM
OK, I see. Depends how you’re starting your service. If you’re able to new it up directly in the test and inject the base URL/port into it, that’s generally simplest. Alternatively you could go for a fixed port number e.g.
@WireMockTest(httpPort = 8888)
then configure your service to use this. This prevents test parallelisation, although the fact your service is also on a fixed port precludes this anyway.
k

Kushagra Kapoor

06/13/2023, 12:10 PM
My intent is to use the parallelism as earlier I was using static port and the test were very slow as they were not parallel
@Robert Strauch Since you have used @DynamicPropertySource in your test, could you please comment here? Did you try any other way to achieve this or was it just through this spring based annotation?
z

Zahid Khan

06/13/2023, 3:36 PM
Hi You can achieve it by something
Copy code
```
@DynamicPropertySource
@SuppressWarnings("unused")
static void registerProperties(DynamicPropertyRegistry registry) {
  registry.add(
      "url.myapp.endpoint",
      () -> "<http://localhost>:" + _PORT_ + "/test");
}
```
k

Kushagra Kapoor

06/13/2023, 3:37 PM
@Zahid Khan Yes trying by that. Only concern with this is, this annotation is spring bound and I was looking for something wiremock specific.
266 Views