英文:
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
<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 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 :
<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>
<!-- you must add this line -->
<argLine>-Dservice.port=${service.port}</argLine>
</configuration>
</execution>
</executions>
</plugin>
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!
<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>
<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>
<!-- Surefire configuration -->
</configuration>
</execution>
</executions>
</plugin>
good luck !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论