Set property with wiremock random port in spring boot test












4















I have a Spring Boot test that uses wiremock to mock an external service. In order to avoid conflicts with parallel builds I don't want to set a fixed port number for wiremock and would like to rely on its dynamic port configuration.



The application uses a property (external.baseUrl) set in the application.yml (under src/test/resources). However I didn't find a way to programmatically override that. I've tried something like this:



    WireMockServer wireMockServer = new WireMockServer();
wireMockServer.start();
WireMock mockClient = new WireMock("localhost", wireMockServer.port());
System.setProperty("external.baseUrl", "http://localhost:" + wireMockServer.port());


but it didn't work and the value in application.yml was used instead. All other solutions that I've looked at override the property with a static value (for example in some annotation), but I don't know the value of the wiremock port until the test is run.



Clarification:



Both spring boot and wiremock run on random ports. That's fine and I know how to get the value of both ports. However wiremock is supposed to mock an external service and I need to tell my application how to reach it. I do this with the external.baseUrl property. The value I want to set in my test depends of course on the wiremock port number. My problem is simply how to programmatically set a property in a spring boot test.










share|improve this question





























    4















    I have a Spring Boot test that uses wiremock to mock an external service. In order to avoid conflicts with parallel builds I don't want to set a fixed port number for wiremock and would like to rely on its dynamic port configuration.



    The application uses a property (external.baseUrl) set in the application.yml (under src/test/resources). However I didn't find a way to programmatically override that. I've tried something like this:



        WireMockServer wireMockServer = new WireMockServer();
    wireMockServer.start();
    WireMock mockClient = new WireMock("localhost", wireMockServer.port());
    System.setProperty("external.baseUrl", "http://localhost:" + wireMockServer.port());


    but it didn't work and the value in application.yml was used instead. All other solutions that I've looked at override the property with a static value (for example in some annotation), but I don't know the value of the wiremock port until the test is run.



    Clarification:



    Both spring boot and wiremock run on random ports. That's fine and I know how to get the value of both ports. However wiremock is supposed to mock an external service and I need to tell my application how to reach it. I do this with the external.baseUrl property. The value I want to set in my test depends of course on the wiremock port number. My problem is simply how to programmatically set a property in a spring boot test.










    share|improve this question



























      4












      4








      4








      I have a Spring Boot test that uses wiremock to mock an external service. In order to avoid conflicts with parallel builds I don't want to set a fixed port number for wiremock and would like to rely on its dynamic port configuration.



      The application uses a property (external.baseUrl) set in the application.yml (under src/test/resources). However I didn't find a way to programmatically override that. I've tried something like this:



          WireMockServer wireMockServer = new WireMockServer();
      wireMockServer.start();
      WireMock mockClient = new WireMock("localhost", wireMockServer.port());
      System.setProperty("external.baseUrl", "http://localhost:" + wireMockServer.port());


      but it didn't work and the value in application.yml was used instead. All other solutions that I've looked at override the property with a static value (for example in some annotation), but I don't know the value of the wiremock port until the test is run.



      Clarification:



      Both spring boot and wiremock run on random ports. That's fine and I know how to get the value of both ports. However wiremock is supposed to mock an external service and I need to tell my application how to reach it. I do this with the external.baseUrl property. The value I want to set in my test depends of course on the wiremock port number. My problem is simply how to programmatically set a property in a spring boot test.










      share|improve this question
















      I have a Spring Boot test that uses wiremock to mock an external service. In order to avoid conflicts with parallel builds I don't want to set a fixed port number for wiremock and would like to rely on its dynamic port configuration.



      The application uses a property (external.baseUrl) set in the application.yml (under src/test/resources). However I didn't find a way to programmatically override that. I've tried something like this:



          WireMockServer wireMockServer = new WireMockServer();
      wireMockServer.start();
      WireMock mockClient = new WireMock("localhost", wireMockServer.port());
      System.setProperty("external.baseUrl", "http://localhost:" + wireMockServer.port());


      but it didn't work and the value in application.yml was used instead. All other solutions that I've looked at override the property with a static value (for example in some annotation), but I don't know the value of the wiremock port until the test is run.



      Clarification:



      Both spring boot and wiremock run on random ports. That's fine and I know how to get the value of both ports. However wiremock is supposed to mock an external service and I need to tell my application how to reach it. I do this with the external.baseUrl property. The value I want to set in my test depends of course on the wiremock port number. My problem is simply how to programmatically set a property in a spring boot test.







      java spring-boot configuration wiremock spring-boot-test






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 17 '18 at 18:15







      bangnab

















      asked Feb 9 '18 at 14:02









      bangnabbangnab

      1,18221425




      1,18221425
























          6 Answers
          6






          active

          oldest

          votes


















          1














          Consider using Spring Cloud Contract Wiremock



          There is already JUnit Rule builder which allows to specify ${wiremock.port}
          to set random port in property/yaml files



          Or you can use WireMockRestServiceServer to bind wiremock to your RestTemplate so you don't even need to override url in your tests






          share|improve this answer
























          • This is gonna be a best answer

            – Roman Sinyakov
            Jan 9 at 12:14











          • This is exactly what I was looking for! I knew that there should be a standard solution for such a common problem!

            – bangnab
            2 days ago



















          4














          I could not find a way to override properties in a Spring Boot integration test, since the test is run only after the application is created and all the beans already configured.



          As a work around I added a @TestConfiguration to the test to replace the beans in the application:



          private static WireMockServer wireMockServer1 = getWireMockServer();
          private static WireMockServer wireMockServer2 = getWireMockServer();
          private static WireMockServer wireMockServer3 = getWireMockServer();

          private static WireMockServer getWireMockServer() {
          final WireMockServer wireMockServer = new WireMockServer(options().dynamicPort());
          wireMockServer.start();
          return wireMockServer;
          }

          @TestConfiguration
          static class TestConfig {
          @Bean
          @Primary
          public BeanUsingAProperty1 getBean1() {
          BeanUsingAProperty myBean = new BeanUsingAProperty();
          myBean.setPort(wireMockServer.port());
          return myBean;
          }

          @Bean
          @Primary
          public BeanUsingAProperty2 getBean2() {
          String baseUrl = "http://localhost:" + wireMockServer2.port();
          return new BeanUsingAProperty2(baseUrl);
          }

          @Bean
          @Primary
          public BeanUsingAProperty3 getBean3() {
          String baseUrl = "http://localhost:" + wireMockServer3.port() + "/request";
          return new BeanUsingAProperty3(new RestTemplate(), baseUrl, "someOtherParameter");
          }
          }


          This effectively replaced the BeanUsingAProperty with the one defined in the test so that it has the correct port number for Wiremock.



          For this configuration to be picked up I had to add this class in the test annotation



          @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {
          MySpringBootApplication.class, MyIntegrationTest.TestConfig.class })


          Note that I use the non-static Wiremock API, since I have several such external services that each need to be mocked. Note that how the different beans are built is different depending on how each was designed.






          share|improve this answer

































            1














            Use property substitution in your application.properties:



            external.baseUrl=http://exampleUrl:${wiremock.server.port}



            This requires the wiremock.server.port property to be set before the SpringBootTest is initialised, which can be achieved by adding the @AutoConfigureWireMock annotation to your test class.






            share|improve this answer





















            • 1





              This won't work if the wiremock.server.port is only defined at runtime.

              – bangnab
              Jan 17 at 9:04











            • Will alter my answer to include a prerequisite

              – user3302637
              2 days ago



















            0














            What about this:



            @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
            class YourTestClass {
            @LocalServerPort
            int port;

            public void test() {
            WireMockServer wireMockServer = new WireMockServer(port);
            wireMockServer.start();
            WireMock mockClient = new WireMock("localhost", port);
            }
            }





            share|improve this answer



















            • 1





              This simply uses the same port for the spring app and wiremock. What I need is to configure the spring app to use the wiremock port when it's making call to an external endpoint. The spring app acts as a client to the service mocked by wiremock.

              – bangnab
              Feb 9 '18 at 19:58






            • 2





              @inovaovao Doesn't this actually accomplish what you need though? If SpringBootTest assigns a random port, then it shouldn't conflict with parallel build jobs correct?

              – entpnerd
              Feb 13 '18 at 5:16











            • @entpnerd I have no problem making both spring boot and wiremock using random ports (guaranteed free). But actually you don't want it do be the same port (as you seem to be doing)! Anyhow tha't not my problem. Read the clarification.

              – bangnab
              Feb 18 '18 at 19:00





















            0














            The approach I use to programmatically change a property when starting a Spring Boot app, is to pass the custom value into the application main entry-point String args. This will have the effect of over-riding all other means such as System properties, YML or other config files.



            Here is an example:



            String args = new String{"--my.prop=foo"};
            SpringApplication.run(Application.class, args);


            It will be easy for you to expose a static method or custom API which starts the Spring Boot app (for testing) and with the value you want.



            And then, once you have the value of the wiremock port - things are easy. Here is an example: PaymentServiceContractTest.java



            P.S. Karate (the open-source test examples I am using above) is a new alternative to WireMock, do check it out ;)






            share|improve this answer































              -1














              How are you reading external.baseUrl?
              If you are using a @Value annotated property, you can use ReflectionTestUtils to set the port after you have setup the mock server.



              ReflectionTestUtils.setField(yourTestClass, "youPort",  wireMockServer.port());





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48707625%2fset-property-with-wiremock-random-port-in-spring-boot-test%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                6 Answers
                6






                active

                oldest

                votes








                6 Answers
                6






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                Consider using Spring Cloud Contract Wiremock



                There is already JUnit Rule builder which allows to specify ${wiremock.port}
                to set random port in property/yaml files



                Or you can use WireMockRestServiceServer to bind wiremock to your RestTemplate so you don't even need to override url in your tests






                share|improve this answer
























                • This is gonna be a best answer

                  – Roman Sinyakov
                  Jan 9 at 12:14











                • This is exactly what I was looking for! I knew that there should be a standard solution for such a common problem!

                  – bangnab
                  2 days ago
















                1














                Consider using Spring Cloud Contract Wiremock



                There is already JUnit Rule builder which allows to specify ${wiremock.port}
                to set random port in property/yaml files



                Or you can use WireMockRestServiceServer to bind wiremock to your RestTemplate so you don't even need to override url in your tests






                share|improve this answer
























                • This is gonna be a best answer

                  – Roman Sinyakov
                  Jan 9 at 12:14











                • This is exactly what I was looking for! I knew that there should be a standard solution for such a common problem!

                  – bangnab
                  2 days ago














                1












                1








                1







                Consider using Spring Cloud Contract Wiremock



                There is already JUnit Rule builder which allows to specify ${wiremock.port}
                to set random port in property/yaml files



                Or you can use WireMockRestServiceServer to bind wiremock to your RestTemplate so you don't even need to override url in your tests






                share|improve this answer













                Consider using Spring Cloud Contract Wiremock



                There is already JUnit Rule builder which allows to specify ${wiremock.port}
                to set random port in property/yaml files



                Or you can use WireMockRestServiceServer to bind wiremock to your RestTemplate so you don't even need to override url in your tests







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 19 '18 at 4:30









                DagonDagon

                8110




                8110













                • This is gonna be a best answer

                  – Roman Sinyakov
                  Jan 9 at 12:14











                • This is exactly what I was looking for! I knew that there should be a standard solution for such a common problem!

                  – bangnab
                  2 days ago



















                • This is gonna be a best answer

                  – Roman Sinyakov
                  Jan 9 at 12:14











                • This is exactly what I was looking for! I knew that there should be a standard solution for such a common problem!

                  – bangnab
                  2 days ago

















                This is gonna be a best answer

                – Roman Sinyakov
                Jan 9 at 12:14





                This is gonna be a best answer

                – Roman Sinyakov
                Jan 9 at 12:14













                This is exactly what I was looking for! I knew that there should be a standard solution for such a common problem!

                – bangnab
                2 days ago





                This is exactly what I was looking for! I knew that there should be a standard solution for such a common problem!

                – bangnab
                2 days ago













                4














                I could not find a way to override properties in a Spring Boot integration test, since the test is run only after the application is created and all the beans already configured.



                As a work around I added a @TestConfiguration to the test to replace the beans in the application:



                private static WireMockServer wireMockServer1 = getWireMockServer();
                private static WireMockServer wireMockServer2 = getWireMockServer();
                private static WireMockServer wireMockServer3 = getWireMockServer();

                private static WireMockServer getWireMockServer() {
                final WireMockServer wireMockServer = new WireMockServer(options().dynamicPort());
                wireMockServer.start();
                return wireMockServer;
                }

                @TestConfiguration
                static class TestConfig {
                @Bean
                @Primary
                public BeanUsingAProperty1 getBean1() {
                BeanUsingAProperty myBean = new BeanUsingAProperty();
                myBean.setPort(wireMockServer.port());
                return myBean;
                }

                @Bean
                @Primary
                public BeanUsingAProperty2 getBean2() {
                String baseUrl = "http://localhost:" + wireMockServer2.port();
                return new BeanUsingAProperty2(baseUrl);
                }

                @Bean
                @Primary
                public BeanUsingAProperty3 getBean3() {
                String baseUrl = "http://localhost:" + wireMockServer3.port() + "/request";
                return new BeanUsingAProperty3(new RestTemplate(), baseUrl, "someOtherParameter");
                }
                }


                This effectively replaced the BeanUsingAProperty with the one defined in the test so that it has the correct port number for Wiremock.



                For this configuration to be picked up I had to add this class in the test annotation



                @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {
                MySpringBootApplication.class, MyIntegrationTest.TestConfig.class })


                Note that I use the non-static Wiremock API, since I have several such external services that each need to be mocked. Note that how the different beans are built is different depending on how each was designed.






                share|improve this answer






























                  4














                  I could not find a way to override properties in a Spring Boot integration test, since the test is run only after the application is created and all the beans already configured.



                  As a work around I added a @TestConfiguration to the test to replace the beans in the application:



                  private static WireMockServer wireMockServer1 = getWireMockServer();
                  private static WireMockServer wireMockServer2 = getWireMockServer();
                  private static WireMockServer wireMockServer3 = getWireMockServer();

                  private static WireMockServer getWireMockServer() {
                  final WireMockServer wireMockServer = new WireMockServer(options().dynamicPort());
                  wireMockServer.start();
                  return wireMockServer;
                  }

                  @TestConfiguration
                  static class TestConfig {
                  @Bean
                  @Primary
                  public BeanUsingAProperty1 getBean1() {
                  BeanUsingAProperty myBean = new BeanUsingAProperty();
                  myBean.setPort(wireMockServer.port());
                  return myBean;
                  }

                  @Bean
                  @Primary
                  public BeanUsingAProperty2 getBean2() {
                  String baseUrl = "http://localhost:" + wireMockServer2.port();
                  return new BeanUsingAProperty2(baseUrl);
                  }

                  @Bean
                  @Primary
                  public BeanUsingAProperty3 getBean3() {
                  String baseUrl = "http://localhost:" + wireMockServer3.port() + "/request";
                  return new BeanUsingAProperty3(new RestTemplate(), baseUrl, "someOtherParameter");
                  }
                  }


                  This effectively replaced the BeanUsingAProperty with the one defined in the test so that it has the correct port number for Wiremock.



                  For this configuration to be picked up I had to add this class in the test annotation



                  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {
                  MySpringBootApplication.class, MyIntegrationTest.TestConfig.class })


                  Note that I use the non-static Wiremock API, since I have several such external services that each need to be mocked. Note that how the different beans are built is different depending on how each was designed.






                  share|improve this answer




























                    4












                    4








                    4







                    I could not find a way to override properties in a Spring Boot integration test, since the test is run only after the application is created and all the beans already configured.



                    As a work around I added a @TestConfiguration to the test to replace the beans in the application:



                    private static WireMockServer wireMockServer1 = getWireMockServer();
                    private static WireMockServer wireMockServer2 = getWireMockServer();
                    private static WireMockServer wireMockServer3 = getWireMockServer();

                    private static WireMockServer getWireMockServer() {
                    final WireMockServer wireMockServer = new WireMockServer(options().dynamicPort());
                    wireMockServer.start();
                    return wireMockServer;
                    }

                    @TestConfiguration
                    static class TestConfig {
                    @Bean
                    @Primary
                    public BeanUsingAProperty1 getBean1() {
                    BeanUsingAProperty myBean = new BeanUsingAProperty();
                    myBean.setPort(wireMockServer.port());
                    return myBean;
                    }

                    @Bean
                    @Primary
                    public BeanUsingAProperty2 getBean2() {
                    String baseUrl = "http://localhost:" + wireMockServer2.port();
                    return new BeanUsingAProperty2(baseUrl);
                    }

                    @Bean
                    @Primary
                    public BeanUsingAProperty3 getBean3() {
                    String baseUrl = "http://localhost:" + wireMockServer3.port() + "/request";
                    return new BeanUsingAProperty3(new RestTemplate(), baseUrl, "someOtherParameter");
                    }
                    }


                    This effectively replaced the BeanUsingAProperty with the one defined in the test so that it has the correct port number for Wiremock.



                    For this configuration to be picked up I had to add this class in the test annotation



                    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {
                    MySpringBootApplication.class, MyIntegrationTest.TestConfig.class })


                    Note that I use the non-static Wiremock API, since I have several such external services that each need to be mocked. Note that how the different beans are built is different depending on how each was designed.






                    share|improve this answer















                    I could not find a way to override properties in a Spring Boot integration test, since the test is run only after the application is created and all the beans already configured.



                    As a work around I added a @TestConfiguration to the test to replace the beans in the application:



                    private static WireMockServer wireMockServer1 = getWireMockServer();
                    private static WireMockServer wireMockServer2 = getWireMockServer();
                    private static WireMockServer wireMockServer3 = getWireMockServer();

                    private static WireMockServer getWireMockServer() {
                    final WireMockServer wireMockServer = new WireMockServer(options().dynamicPort());
                    wireMockServer.start();
                    return wireMockServer;
                    }

                    @TestConfiguration
                    static class TestConfig {
                    @Bean
                    @Primary
                    public BeanUsingAProperty1 getBean1() {
                    BeanUsingAProperty myBean = new BeanUsingAProperty();
                    myBean.setPort(wireMockServer.port());
                    return myBean;
                    }

                    @Bean
                    @Primary
                    public BeanUsingAProperty2 getBean2() {
                    String baseUrl = "http://localhost:" + wireMockServer2.port();
                    return new BeanUsingAProperty2(baseUrl);
                    }

                    @Bean
                    @Primary
                    public BeanUsingAProperty3 getBean3() {
                    String baseUrl = "http://localhost:" + wireMockServer3.port() + "/request";
                    return new BeanUsingAProperty3(new RestTemplate(), baseUrl, "someOtherParameter");
                    }
                    }


                    This effectively replaced the BeanUsingAProperty with the one defined in the test so that it has the correct port number for Wiremock.



                    For this configuration to be picked up I had to add this class in the test annotation



                    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {
                    MySpringBootApplication.class, MyIntegrationTest.TestConfig.class })


                    Note that I use the non-static Wiremock API, since I have several such external services that each need to be mocked. Note that how the different beans are built is different depending on how each was designed.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 19 '18 at 12:38

























                    answered Feb 19 '18 at 12:24









                    bangnabbangnab

                    1,18221425




                    1,18221425























                        1














                        Use property substitution in your application.properties:



                        external.baseUrl=http://exampleUrl:${wiremock.server.port}



                        This requires the wiremock.server.port property to be set before the SpringBootTest is initialised, which can be achieved by adding the @AutoConfigureWireMock annotation to your test class.






                        share|improve this answer





















                        • 1





                          This won't work if the wiremock.server.port is only defined at runtime.

                          – bangnab
                          Jan 17 at 9:04











                        • Will alter my answer to include a prerequisite

                          – user3302637
                          2 days ago
















                        1














                        Use property substitution in your application.properties:



                        external.baseUrl=http://exampleUrl:${wiremock.server.port}



                        This requires the wiremock.server.port property to be set before the SpringBootTest is initialised, which can be achieved by adding the @AutoConfigureWireMock annotation to your test class.






                        share|improve this answer





















                        • 1





                          This won't work if the wiremock.server.port is only defined at runtime.

                          – bangnab
                          Jan 17 at 9:04











                        • Will alter my answer to include a prerequisite

                          – user3302637
                          2 days ago














                        1












                        1








                        1







                        Use property substitution in your application.properties:



                        external.baseUrl=http://exampleUrl:${wiremock.server.port}



                        This requires the wiremock.server.port property to be set before the SpringBootTest is initialised, which can be achieved by adding the @AutoConfigureWireMock annotation to your test class.






                        share|improve this answer















                        Use property substitution in your application.properties:



                        external.baseUrl=http://exampleUrl:${wiremock.server.port}



                        This requires the wiremock.server.port property to be set before the SpringBootTest is initialised, which can be achieved by adding the @AutoConfigureWireMock annotation to your test class.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited 2 days ago

























                        answered Jan 16 at 12:21









                        user3302637user3302637

                        213




                        213








                        • 1





                          This won't work if the wiremock.server.port is only defined at runtime.

                          – bangnab
                          Jan 17 at 9:04











                        • Will alter my answer to include a prerequisite

                          – user3302637
                          2 days ago














                        • 1





                          This won't work if the wiremock.server.port is only defined at runtime.

                          – bangnab
                          Jan 17 at 9:04











                        • Will alter my answer to include a prerequisite

                          – user3302637
                          2 days ago








                        1




                        1





                        This won't work if the wiremock.server.port is only defined at runtime.

                        – bangnab
                        Jan 17 at 9:04





                        This won't work if the wiremock.server.port is only defined at runtime.

                        – bangnab
                        Jan 17 at 9:04













                        Will alter my answer to include a prerequisite

                        – user3302637
                        2 days ago





                        Will alter my answer to include a prerequisite

                        – user3302637
                        2 days ago











                        0














                        What about this:



                        @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
                        class YourTestClass {
                        @LocalServerPort
                        int port;

                        public void test() {
                        WireMockServer wireMockServer = new WireMockServer(port);
                        wireMockServer.start();
                        WireMock mockClient = new WireMock("localhost", port);
                        }
                        }





                        share|improve this answer



















                        • 1





                          This simply uses the same port for the spring app and wiremock. What I need is to configure the spring app to use the wiremock port when it's making call to an external endpoint. The spring app acts as a client to the service mocked by wiremock.

                          – bangnab
                          Feb 9 '18 at 19:58






                        • 2





                          @inovaovao Doesn't this actually accomplish what you need though? If SpringBootTest assigns a random port, then it shouldn't conflict with parallel build jobs correct?

                          – entpnerd
                          Feb 13 '18 at 5:16











                        • @entpnerd I have no problem making both spring boot and wiremock using random ports (guaranteed free). But actually you don't want it do be the same port (as you seem to be doing)! Anyhow tha't not my problem. Read the clarification.

                          – bangnab
                          Feb 18 '18 at 19:00


















                        0














                        What about this:



                        @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
                        class YourTestClass {
                        @LocalServerPort
                        int port;

                        public void test() {
                        WireMockServer wireMockServer = new WireMockServer(port);
                        wireMockServer.start();
                        WireMock mockClient = new WireMock("localhost", port);
                        }
                        }





                        share|improve this answer



















                        • 1





                          This simply uses the same port for the spring app and wiremock. What I need is to configure the spring app to use the wiremock port when it's making call to an external endpoint. The spring app acts as a client to the service mocked by wiremock.

                          – bangnab
                          Feb 9 '18 at 19:58






                        • 2





                          @inovaovao Doesn't this actually accomplish what you need though? If SpringBootTest assigns a random port, then it shouldn't conflict with parallel build jobs correct?

                          – entpnerd
                          Feb 13 '18 at 5:16











                        • @entpnerd I have no problem making both spring boot and wiremock using random ports (guaranteed free). But actually you don't want it do be the same port (as you seem to be doing)! Anyhow tha't not my problem. Read the clarification.

                          – bangnab
                          Feb 18 '18 at 19:00
















                        0












                        0








                        0







                        What about this:



                        @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
                        class YourTestClass {
                        @LocalServerPort
                        int port;

                        public void test() {
                        WireMockServer wireMockServer = new WireMockServer(port);
                        wireMockServer.start();
                        WireMock mockClient = new WireMock("localhost", port);
                        }
                        }





                        share|improve this answer













                        What about this:



                        @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
                        class YourTestClass {
                        @LocalServerPort
                        int port;

                        public void test() {
                        WireMockServer wireMockServer = new WireMockServer(port);
                        wireMockServer.start();
                        WireMock mockClient = new WireMock("localhost", port);
                        }
                        }






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Feb 9 '18 at 15:16









                        luboskrnacluboskrnac

                        15.4k54671




                        15.4k54671








                        • 1





                          This simply uses the same port for the spring app and wiremock. What I need is to configure the spring app to use the wiremock port when it's making call to an external endpoint. The spring app acts as a client to the service mocked by wiremock.

                          – bangnab
                          Feb 9 '18 at 19:58






                        • 2





                          @inovaovao Doesn't this actually accomplish what you need though? If SpringBootTest assigns a random port, then it shouldn't conflict with parallel build jobs correct?

                          – entpnerd
                          Feb 13 '18 at 5:16











                        • @entpnerd I have no problem making both spring boot and wiremock using random ports (guaranteed free). But actually you don't want it do be the same port (as you seem to be doing)! Anyhow tha't not my problem. Read the clarification.

                          – bangnab
                          Feb 18 '18 at 19:00
















                        • 1





                          This simply uses the same port for the spring app and wiremock. What I need is to configure the spring app to use the wiremock port when it's making call to an external endpoint. The spring app acts as a client to the service mocked by wiremock.

                          – bangnab
                          Feb 9 '18 at 19:58






                        • 2





                          @inovaovao Doesn't this actually accomplish what you need though? If SpringBootTest assigns a random port, then it shouldn't conflict with parallel build jobs correct?

                          – entpnerd
                          Feb 13 '18 at 5:16











                        • @entpnerd I have no problem making both spring boot and wiremock using random ports (guaranteed free). But actually you don't want it do be the same port (as you seem to be doing)! Anyhow tha't not my problem. Read the clarification.

                          – bangnab
                          Feb 18 '18 at 19:00










                        1




                        1





                        This simply uses the same port for the spring app and wiremock. What I need is to configure the spring app to use the wiremock port when it's making call to an external endpoint. The spring app acts as a client to the service mocked by wiremock.

                        – bangnab
                        Feb 9 '18 at 19:58





                        This simply uses the same port for the spring app and wiremock. What I need is to configure the spring app to use the wiremock port when it's making call to an external endpoint. The spring app acts as a client to the service mocked by wiremock.

                        – bangnab
                        Feb 9 '18 at 19:58




                        2




                        2





                        @inovaovao Doesn't this actually accomplish what you need though? If SpringBootTest assigns a random port, then it shouldn't conflict with parallel build jobs correct?

                        – entpnerd
                        Feb 13 '18 at 5:16





                        @inovaovao Doesn't this actually accomplish what you need though? If SpringBootTest assigns a random port, then it shouldn't conflict with parallel build jobs correct?

                        – entpnerd
                        Feb 13 '18 at 5:16













                        @entpnerd I have no problem making both spring boot and wiremock using random ports (guaranteed free). But actually you don't want it do be the same port (as you seem to be doing)! Anyhow tha't not my problem. Read the clarification.

                        – bangnab
                        Feb 18 '18 at 19:00







                        @entpnerd I have no problem making both spring boot and wiremock using random ports (guaranteed free). But actually you don't want it do be the same port (as you seem to be doing)! Anyhow tha't not my problem. Read the clarification.

                        – bangnab
                        Feb 18 '18 at 19:00













                        0














                        The approach I use to programmatically change a property when starting a Spring Boot app, is to pass the custom value into the application main entry-point String args. This will have the effect of over-riding all other means such as System properties, YML or other config files.



                        Here is an example:



                        String args = new String{"--my.prop=foo"};
                        SpringApplication.run(Application.class, args);


                        It will be easy for you to expose a static method or custom API which starts the Spring Boot app (for testing) and with the value you want.



                        And then, once you have the value of the wiremock port - things are easy. Here is an example: PaymentServiceContractTest.java



                        P.S. Karate (the open-source test examples I am using above) is a new alternative to WireMock, do check it out ;)






                        share|improve this answer




























                          0














                          The approach I use to programmatically change a property when starting a Spring Boot app, is to pass the custom value into the application main entry-point String args. This will have the effect of over-riding all other means such as System properties, YML or other config files.



                          Here is an example:



                          String args = new String{"--my.prop=foo"};
                          SpringApplication.run(Application.class, args);


                          It will be easy for you to expose a static method or custom API which starts the Spring Boot app (for testing) and with the value you want.



                          And then, once you have the value of the wiremock port - things are easy. Here is an example: PaymentServiceContractTest.java



                          P.S. Karate (the open-source test examples I am using above) is a new alternative to WireMock, do check it out ;)






                          share|improve this answer


























                            0












                            0








                            0







                            The approach I use to programmatically change a property when starting a Spring Boot app, is to pass the custom value into the application main entry-point String args. This will have the effect of over-riding all other means such as System properties, YML or other config files.



                            Here is an example:



                            String args = new String{"--my.prop=foo"};
                            SpringApplication.run(Application.class, args);


                            It will be easy for you to expose a static method or custom API which starts the Spring Boot app (for testing) and with the value you want.



                            And then, once you have the value of the wiremock port - things are easy. Here is an example: PaymentServiceContractTest.java



                            P.S. Karate (the open-source test examples I am using above) is a new alternative to WireMock, do check it out ;)






                            share|improve this answer













                            The approach I use to programmatically change a property when starting a Spring Boot app, is to pass the custom value into the application main entry-point String args. This will have the effect of over-riding all other means such as System properties, YML or other config files.



                            Here is an example:



                            String args = new String{"--my.prop=foo"};
                            SpringApplication.run(Application.class, args);


                            It will be easy for you to expose a static method or custom API which starts the Spring Boot app (for testing) and with the value you want.



                            And then, once you have the value of the wiremock port - things are easy. Here is an example: PaymentServiceContractTest.java



                            P.S. Karate (the open-source test examples I am using above) is a new alternative to WireMock, do check it out ;)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 13 '18 at 14:17









                            Peter ThomasPeter Thomas

                            13.2k31844




                            13.2k31844























                                -1














                                How are you reading external.baseUrl?
                                If you are using a @Value annotated property, you can use ReflectionTestUtils to set the port after you have setup the mock server.



                                ReflectionTestUtils.setField(yourTestClass, "youPort",  wireMockServer.port());





                                share|improve this answer




























                                  -1














                                  How are you reading external.baseUrl?
                                  If you are using a @Value annotated property, you can use ReflectionTestUtils to set the port after you have setup the mock server.



                                  ReflectionTestUtils.setField(yourTestClass, "youPort",  wireMockServer.port());





                                  share|improve this answer


























                                    -1












                                    -1








                                    -1







                                    How are you reading external.baseUrl?
                                    If you are using a @Value annotated property, you can use ReflectionTestUtils to set the port after you have setup the mock server.



                                    ReflectionTestUtils.setField(yourTestClass, "youPort",  wireMockServer.port());





                                    share|improve this answer













                                    How are you reading external.baseUrl?
                                    If you are using a @Value annotated property, you can use ReflectionTestUtils to set the port after you have setup the mock server.



                                    ReflectionTestUtils.setField(yourTestClass, "youPort",  wireMockServer.port());






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Feb 13 '18 at 15:07









                                    redhunterredhunter

                                    125




                                    125






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48707625%2fset-property-with-wiremock-random-port-in-spring-boot-test%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Liquibase includeAll doesn't find base path

                                        How to use setInterval in EJS file?

                                        Petrus Granier-Deferre