0

I have a multi module spring-boot project, before integration tests of my app, I start another child module (which is Stub made by another spring boot app) You can see it is attached to "pre-integration-test" and it is working fine finally.

 Parent Pom
|   
|----myRealApp module(spring boot app)
|----stub module(This is also a spring-boot app)

My question is, is there a way to randomize And share this port (not fixed to 8090), so concurrent builds on Jenkins server can run tests and not fail because address is in use already.

I know I can generate random numbers/ports in spring properties file. But couldn't find a way to pass it to Pom.

application-test.properties of myRealApp:

 stub.port=8090
 stub.url=http://localhost:${stub.port}/stub/api/v1/domains/

Pom of myRealApp:

      <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>${spring.boot.mainclass}</mainClass>
            </configuration>
            <executions>
                <execution>
                    <id>start-stub</id>
                    <configuration>
                        <arguments>
                            <argument>--server.port=8090</argument>
                        </arguments>
                        <mainClass>io.swagger.Stub</mainClass>
                        <classesDirectory>../my-stub/target/classes</classesDirectory>
                    </configuration>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <phase>pre-integration-test</phase>
                </execution>

                <execution>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>
      </plugin>
Spring
  • 11,333
  • 29
  • 116
  • 185
  • I'm not clear why you need a stub app for testing? Usually in Spring Boot it's really easy to write unit/integration tests etc. – khmarbaise Aug 20 '18 at 17:19
  • @khmarbaise Keep saying "Spring Boot it's really easy to write tests" is not solving any problem. MockRestServerServer does configure the RestTemplate with a custom ClientHttpRequestFactory, thus overrides my custom requestFactory, which has settings for timeouts and authentication. Can you please explain how do you plan to test these? – Spring Aug 21 '18 at 09:52

2 Answers2

2

You can do that via jenkins Port Allocator Plugin

Once you assign the port (lets say HTTP_PORT), then you can pass this as command line

-Dstub.port=$HTTP_PORT
NullPointerException
  • 3,732
  • 5
  • 28
  • 62
0

I recommend you not to randomize at all. My suggestion is to parametrize the server port in the POM and application-test.properties files, and set a value based upon some Jenkins-provided variable: For example, BUILD_NUMBER, which is incremented on every build and thus uniqueness is guranteed.

However, there is a problem about this: You also need to enclose the port number within valid boundaries: TCP ports must be within 1024 and 65535, however BUILD_NUMBER is not limited at all.

How to cope with this? I think a simple Ant task bound to the initialize phase could read the BUILD_NUMBER value, apply it a simple formula 1024+(BUILD_NUMBER % 64512), and set it as the definitive port number variable, which is the one you will reference in the POM and application-test.properties files.

Little Santi
  • 8,563
  • 2
  • 18
  • 46