在Java Cucumber测试中使用Maven随机端口。

huangapple go评论180阅读模式
英文:

Use maven random port in java cucumber test

问题

I'm using this maven plugin to create a random port for a service and Kafka:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.4.0</version>
    <executions>
        <execution>
            <id>reserve-network-port</id>
            <goals>
                <goal>reserve-network-port</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
                <portNames>
                    <portName>kafka.port</portName>
                    <portName>service.port</portName>
                </portNames>
            </configuration>
        </execution>
    </executions>
</plugin>

Then I'm using surfire to run some Integration test:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <executions>
        <execution>
            <id>cucumber-tests</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <systemPropertyVariables>
                    <log.dir>${project.build.directory}/logs</log.dir>
                </systemPropertyVariables>
                <includes>
                    <include>**/*Test.java</include>
                    <include>**/*Steps.java</include>
                    <excludes>**/*cucumber.RunCucumberIT.java</excludes>
                </includes>
                <testFailureIgnore>false</testFailureIgnore>
            </configuration>
        </execution>
    </executions>
</plugin>

Then in my Cucumber step I'm trying to get it through:

var port = System.getProperty("service.port")

But I'm getting a null value. Shall I need to set it in the surefire plugin to be visible in my cucumber execution?

英文:

I'm using this maven plugin to create a random port for a service and Kafka

				&lt;plugin&gt;
					&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
					&lt;artifactId&gt;build-helper-maven-plugin&lt;/artifactId&gt;
					&lt;version&gt;3.4.0&lt;/version&gt;
					&lt;executions&gt;
						&lt;execution&gt;
							&lt;id&gt;reserve-network-port&lt;/id&gt;
							&lt;goals&gt;
								&lt;goal&gt;reserve-network-port&lt;/goal&gt;
							&lt;/goals&gt;
							&lt;phase&gt;process-resources&lt;/phase&gt;
							&lt;configuration&gt;
								&lt;portNames&gt;
									&lt;portName&gt;kafka.port&lt;/portName&gt;
									&lt;portName&gt;service.port&lt;/portName&gt;
								&lt;/portNames&gt;
							&lt;/configuration&gt;
						&lt;/execution&gt;
					&lt;/executions&gt;
				&lt;/plugin&gt;

Then I'm using surfire to run some Integration test

				&lt;plugin&gt;
					&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
					&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
					&lt;version&gt;2.20&lt;/version&gt;
					&lt;executions&gt;
						&lt;execution&gt;
							&lt;id&gt;cucumber-tests&lt;/id&gt;
							&lt;phase&gt;integration-test&lt;/phase&gt;
							&lt;goals&gt;
								&lt;goal&gt;test&lt;/goal&gt;
							&lt;/goals&gt;
							&lt;configuration&gt;
								&lt;systemPropertyVariables&gt;
									&lt;log.dir&gt;${project.build.directory}/logs&lt;/log.dir&gt;
								&lt;/systemPropertyVariables&gt;
								&lt;includes&gt;
									&lt;include&gt;**/*Test.java&lt;/include&gt;
									&lt;include&gt;**/*Steps.java&lt;/include&gt;
									&lt;excludes&gt;**/*cucumber.RunCucumberIT.java&lt;/excludes&gt;
								&lt;/includes&gt;
								&lt;testFailureIgnore&gt;false&lt;/testFailureIgnore&gt;
							&lt;/configuration&gt;
						&lt;/execution&gt;
					&lt;/executions&gt;
				&lt;/plugin&gt;

Then in my Cucumber step I'm trying to get it through

var port = System.getProperty(&quot;service.port&quot;)

But I'm getting null value

Shall I need to set in the surefire plugin to be visible in my cucumber execution?

答案1

得分: 1

    <插件>
        <组Id>org.apache.maven.plugins</组Id>
        <工件Id>maven-surefire-plugin</工件Id>
        <版本>2.20</版本>
        <执行>
            <执行>
                <Id>cucumber-tests</Id>
                <阶段>integration-test</阶段>
                <目标>
                    <目标>test</目标>
                </目标>
                <配置>
                    <系统属性变量>
                        <log.dir>${project.build.directory}/logs</log.dir>
                    </系统属性变量>
                    <包括>
                        <包括>**/*Test.java</包括>
                        <包括>**/*Steps.java</包括>
                        <排除>**/*cucumber.RunCucumberIT.java</排除>
                    </包括>
                    <测试失败忽略>false</测试失败忽略>
                    <!-- 你必须添加这一行 -->
                    <argLine>-Dservice.port=${service.port}</argLine> 
                </配置>
            </执行>
        </执行>
    </插件>

**更新**<br>
你可以尝试将 `build-helper-maven-plugin` 的执行移到 `maven-surefire-plugin` 之前在你的 POM 文件中!

    <插件>
        <组Id>org.codehaus.mojo</组Id>
        <工件Id>build-helper-maven-plugin</工件Id>
        <版本>3.4.0</版本>
        <执行>
            <执行>
                <Id>reserve-network-port</Id>
                <目标>
                    <目标>reserve-network-port</目标>
                </目标>
                <阶段>process-resources</阶段>
                <配置>
                    <端口名称>
                        <端口名称>kafka.port</端口名称>
                        <端口名称>service.port</端口名称>
                    </端口名称>
                </配置>
            </执行>
        </执行>
    </插件>
    
    <插件>
        <组Id>org.apache.maven.plugins</组Id>
        <工件Id>maven-surefire-plugin</工件Id>
        <版本>2.20</版本>
        <执行>
            <执行>
                <Id>cucumber-tests</Id>
                <阶段>integration-test</阶段>
                <目标>
                    <目标>test</目标>
                </目标>
                <配置>
                    <!-- Surefire 配置 -->
                </配置>
            </执行>
        </执行>
    </插件>

祝你好运!
英文:

check this out :

&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;version&gt;2.20&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;id&gt;cucumber-tests&lt;/id&gt;
&lt;phase&gt;integration-test&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;test&lt;/goal&gt;
&lt;/goals&gt;
&lt;configuration&gt;
&lt;systemPropertyVariables&gt;
&lt;log.dir&gt;${project.build.directory}/logs&lt;/log.dir&gt;
&lt;/systemPropertyVariables&gt;
&lt;includes&gt;
&lt;include&gt;**/*Test.java&lt;/include&gt;
&lt;include&gt;**/*Steps.java&lt;/include&gt;
&lt;excludes&gt;**/*cucumber.RunCucumberIT.java&lt;/excludes&gt;
&lt;/includes&gt;
&lt;testFailureIgnore&gt;false&lt;/testFailureIgnore&gt;
&lt;!-- you must add this line --&gt;
&lt;argLine&gt;-Dservice.port=${service.port}&lt;/argLine&gt; 
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;

update<br>
you can try moving the execution of the build-helper-maven-plugin before the execution of the maven-surefire-plugin in your POM file!

&lt;plugin&gt;
&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
&lt;artifactId&gt;build-helper-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;3.4.0&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;id&gt;reserve-network-port&lt;/id&gt;
&lt;goals&gt;
&lt;goal&gt;reserve-network-port&lt;/goal&gt;
&lt;/goals&gt;
&lt;phase&gt;process-resources&lt;/phase&gt;
&lt;configuration&gt;
&lt;portNames&gt;
&lt;portName&gt;kafka.port&lt;/portName&gt;
&lt;portName&gt;service.port&lt;/portName&gt;
&lt;/portNames&gt;
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;version&gt;2.20&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;id&gt;cucumber-tests&lt;/id&gt;
&lt;phase&gt;integration-test&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;test&lt;/goal&gt;
&lt;/goals&gt;
&lt;configuration&gt;
&lt;!-- Surefire configuration --&gt;
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;

good luck !

huangapple
  • 本文由 发表于 2023年7月6日 19:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定