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

v09wglhw  于 2023-08-03  发布在  Maven
关注(0)|答案(1)|浏览(103)

我正在使用这个maven插件为服务和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>

字符串
然后我用Surfire运行一些集成测试

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


然后在我的 cucumber 步骤中我试图让它通过

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


但我得到的是null
我需要在surefire插件中设置为在我的 cucumber 执行中可见吗?

omqzjyyz

omqzjyyz1#

看看这个

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

字符串

更新

您可以尝试在POM文件中执行maven-surefire-plugin之前移动build-helper-maven-plugin的执行!

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


祝你好运!

相关问题