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

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

Use maven random port in java cucumber test

问题

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

  1. <plugin>
  2. <groupId>org.codehaus.mojo</groupId>
  3. <artifactId>build-helper-maven-plugin</artifactId>
  4. <version>3.4.0</version>
  5. <executions>
  6. <execution>
  7. <id>reserve-network-port</id>
  8. <goals>
  9. <goal>reserve-network-port</goal>
  10. </goals>
  11. <phase>process-resources</phase>
  12. <configuration>
  13. <portNames>
  14. <portName>kafka.port</portName>
  15. <portName>service.port</portName>
  16. </portNames>
  17. </configuration>
  18. </execution>
  19. </executions>
  20. </plugin>

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

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-surefire-plugin</artifactId>
  4. <version>2.20</version>
  5. <executions>
  6. <execution>
  7. <id>cucumber-tests</id>
  8. <phase>integration-test</phase>
  9. <goals>
  10. <goal>test</goal>
  11. </goals>
  12. <configuration>
  13. <systemPropertyVariables>
  14. <log.dir>${project.build.directory}/logs</log.dir>
  15. </systemPropertyVariables>
  16. <includes>
  17. <include>**/*Test.java</include>
  18. <include>**/*Steps.java</include>
  19. <excludes>**/*cucumber.RunCucumberIT.java</excludes>
  20. </includes>
  21. <testFailureIgnore>false</testFailureIgnore>
  22. </configuration>
  23. </execution>
  24. </executions>
  25. </plugin>

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

  1. 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

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
  3. &lt;artifactId&gt;build-helper-maven-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;3.4.0&lt;/version&gt;
  5. &lt;executions&gt;
  6. &lt;execution&gt;
  7. &lt;id&gt;reserve-network-port&lt;/id&gt;
  8. &lt;goals&gt;
  9. &lt;goal&gt;reserve-network-port&lt;/goal&gt;
  10. &lt;/goals&gt;
  11. &lt;phase&gt;process-resources&lt;/phase&gt;
  12. &lt;configuration&gt;
  13. &lt;portNames&gt;
  14. &lt;portName&gt;kafka.port&lt;/portName&gt;
  15. &lt;portName&gt;service.port&lt;/portName&gt;
  16. &lt;/portNames&gt;
  17. &lt;/configuration&gt;
  18. &lt;/execution&gt;
  19. &lt;/executions&gt;
  20. &lt;/plugin&gt;

Then I'm using surfire to run some Integration test

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  3. &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;2.20&lt;/version&gt;
  5. &lt;executions&gt;
  6. &lt;execution&gt;
  7. &lt;id&gt;cucumber-tests&lt;/id&gt;
  8. &lt;phase&gt;integration-test&lt;/phase&gt;
  9. &lt;goals&gt;
  10. &lt;goal&gt;test&lt;/goal&gt;
  11. &lt;/goals&gt;
  12. &lt;configuration&gt;
  13. &lt;systemPropertyVariables&gt;
  14. &lt;log.dir&gt;${project.build.directory}/logs&lt;/log.dir&gt;
  15. &lt;/systemPropertyVariables&gt;
  16. &lt;includes&gt;
  17. &lt;include&gt;**/*Test.java&lt;/include&gt;
  18. &lt;include&gt;**/*Steps.java&lt;/include&gt;
  19. &lt;excludes&gt;**/*cucumber.RunCucumberIT.java&lt;/excludes&gt;
  20. &lt;/includes&gt;
  21. &lt;testFailureIgnore&gt;false&lt;/testFailureIgnore&gt;
  22. &lt;/configuration&gt;
  23. &lt;/execution&gt;
  24. &lt;/executions&gt;
  25. &lt;/plugin&gt;

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

  1. 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

  1. <插件>
  2. <组Id>org.apache.maven.plugins</组Id>
  3. <工件Id>maven-surefire-plugin</工件Id>
  4. <版本>2.20</版本>
  5. <执行>
  6. <执行>
  7. <Id>cucumber-tests</Id>
  8. <阶段>integration-test</阶段>
  9. <目标>
  10. <目标>test</目标>
  11. </目标>
  12. <配置>
  13. <系统属性变量>
  14. <log.dir>${project.build.directory}/logs</log.dir>
  15. </系统属性变量>
  16. <包括>
  17. <包括>**/*Test.java</包括>
  18. <包括>**/*Steps.java</包括>
  19. <排除>**/*cucumber.RunCucumberIT.java</排除>
  20. </包括>
  21. <测试失败忽略>false</测试失败忽略>
  22. <!-- 你必须添加这一行 -->
  23. <argLine>-Dservice.port=${service.port}</argLine>
  24. </配置>
  25. </执行>
  26. </执行>
  27. </插件>
  28. **更新**<br>
  29. 你可以尝试将 `build-helper-maven-plugin` 的执行移到 `maven-surefire-plugin` 之前在你的 POM 文件中!
  30. <插件>
  31. <组Id>org.codehaus.mojo</组Id>
  32. <工件Id>build-helper-maven-plugin</工件Id>
  33. <版本>3.4.0</版本>
  34. <执行>
  35. <执行>
  36. <Id>reserve-network-port</Id>
  37. <目标>
  38. <目标>reserve-network-port</目标>
  39. </目标>
  40. <阶段>process-resources</阶段>
  41. <配置>
  42. <端口名称>
  43. <端口名称>kafka.port</端口名称>
  44. <端口名称>service.port</端口名称>
  45. </端口名称>
  46. </配置>
  47. </执行>
  48. </执行>
  49. </插件>
  50. <插件>
  51. <组Id>org.apache.maven.plugins</组Id>
  52. <工件Id>maven-surefire-plugin</工件Id>
  53. <版本>2.20</版本>
  54. <执行>
  55. <执行>
  56. <Id>cucumber-tests</Id>
  57. <阶段>integration-test</阶段>
  58. <目标>
  59. <目标>test</目标>
  60. </目标>
  61. <配置>
  62. <!-- Surefire 配置 -->
  63. </配置>
  64. </执行>
  65. </执行>
  66. </插件>
  67. 祝你好运!
英文:

check this out :

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  3. &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;2.20&lt;/version&gt;
  5. &lt;executions&gt;
  6. &lt;execution&gt;
  7. &lt;id&gt;cucumber-tests&lt;/id&gt;
  8. &lt;phase&gt;integration-test&lt;/phase&gt;
  9. &lt;goals&gt;
  10. &lt;goal&gt;test&lt;/goal&gt;
  11. &lt;/goals&gt;
  12. &lt;configuration&gt;
  13. &lt;systemPropertyVariables&gt;
  14. &lt;log.dir&gt;${project.build.directory}/logs&lt;/log.dir&gt;
  15. &lt;/systemPropertyVariables&gt;
  16. &lt;includes&gt;
  17. &lt;include&gt;**/*Test.java&lt;/include&gt;
  18. &lt;include&gt;**/*Steps.java&lt;/include&gt;
  19. &lt;excludes&gt;**/*cucumber.RunCucumberIT.java&lt;/excludes&gt;
  20. &lt;/includes&gt;
  21. &lt;testFailureIgnore&gt;false&lt;/testFailureIgnore&gt;
  22. &lt;!-- you must add this line --&gt;
  23. &lt;argLine&gt;-Dservice.port=${service.port}&lt;/argLine&gt;
  24. &lt;/configuration&gt;
  25. &lt;/execution&gt;
  26. &lt;/executions&gt;
  27. &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!

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
  3. &lt;artifactId&gt;build-helper-maven-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;3.4.0&lt;/version&gt;
  5. &lt;executions&gt;
  6. &lt;execution&gt;
  7. &lt;id&gt;reserve-network-port&lt;/id&gt;
  8. &lt;goals&gt;
  9. &lt;goal&gt;reserve-network-port&lt;/goal&gt;
  10. &lt;/goals&gt;
  11. &lt;phase&gt;process-resources&lt;/phase&gt;
  12. &lt;configuration&gt;
  13. &lt;portNames&gt;
  14. &lt;portName&gt;kafka.port&lt;/portName&gt;
  15. &lt;portName&gt;service.port&lt;/portName&gt;
  16. &lt;/portNames&gt;
  17. &lt;/configuration&gt;
  18. &lt;/execution&gt;
  19. &lt;/executions&gt;
  20. &lt;/plugin&gt;
  21. &lt;plugin&gt;
  22. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  23. &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
  24. &lt;version&gt;2.20&lt;/version&gt;
  25. &lt;executions&gt;
  26. &lt;execution&gt;
  27. &lt;id&gt;cucumber-tests&lt;/id&gt;
  28. &lt;phase&gt;integration-test&lt;/phase&gt;
  29. &lt;goals&gt;
  30. &lt;goal&gt;test&lt;/goal&gt;
  31. &lt;/goals&gt;
  32. &lt;configuration&gt;
  33. &lt;!-- Surefire configuration --&gt;
  34. &lt;/configuration&gt;
  35. &lt;/execution&gt;
  36. &lt;/executions&gt;
  37. &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:

确定