当前位置: 首页 > 知识库问答 >
问题:

如何配置maven插件来运行spock和junit5测试

郎永福
2023-03-14

我想在同一个项目中运行spock和junit5测试。因此,我制作了一个示例项目,在src/main下没有任何内容,但在src/test/java/a/package/下有两个测试。其中一个测试是junit5,另一个是spock测试。

但是在发出mvn test verify之后,只执行JUnit测试,而不执行spock测试。它们通过“运行所有测试”在Intellij中很好地一起运行,所以显然有一些我找不到的配置问题。

下面是我的maven Pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>maven.spock.junit</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <poi.version>3.17</poi.version>
    </properties>

    <dependencies>

        <!-- test dependencies -->

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>


        <!-- spock testing framework -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.0-groovy-2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.4</version>
            <scope>test</scope>
        </dependency>

        <!-- hamcrest matcher -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4.3</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass></mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <!-- mvn test - exclude integration tests -->
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>-Xms2g -Xmx2g -XX:MaxPermSize=500m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.region=US</argLine>
                    <excludes>
                        <exclude>%regex[.*Fixme.*]</exclude>
                        <exclude>%regex[.*(Regression|Integration)Test.*]</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <!-- mvn verify - execute integration tests only -->
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>-Xms2g -Xmx2g -XX:MaxPermSize=500m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.region=US -Duser.timezone=UTC</argLine>
                    <includes>
                        <include>%regex[.*(Regression|Integration)Test.*]</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <sources>
                        <source>
                            <directory>src/test/java</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </source>
                        <source>
                            <directory>src/test/groovy</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </source>
                    </sources>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>skip-unit-tests</id>
            <activation>
                <property>
                    <name>skipUnitTests</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <skipTests>true</skipTests>
                            <argLine>-Duser.timezone=UTC</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>
        <plugin>
            <!-- mvn test - exclude integration tests -->
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
            <configuration>
                <useFile>false</useFile>
                <argLine>-Xms2g -Xmx2g -XX:MaxPermSize=500m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.region=US</argLine>
                <includes>
                    <include>**/*</include>
                </includes>
            </configuration>
        </plugin>

共有1个答案

慕高格
2023-03-14

maven似乎混淆了依赖关系,所以junit依赖关系需要从/project/dependencies转移到surefire和failsafe插件中,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>maven.spock.junit</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <java.version>1.8</java.version>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <maven.compiler.source>${java.version}</maven.compiler.source>

        <groovy.version>2.5.2</groovy.version>
        <junit-platform.version>1.2.0</junit-platform.version>
        <jupiter.version>5.2.0</jupiter.version>
        <spock.version>1.2-groovy-2.5</spock.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>${groovy.version}</version>
            <type>pom</type> <!-- required JUST since Groovy 2.5.0 -->
        </dependency>

        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>${spock.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${jupiter.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <defaultGoal>verify</defaultGoal>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>

            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.6.1</version>
                <configuration>
                    <testSources>
                        <testSource>
                            <directory>src/test/java</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </testSource>
                        <testSource>
                            <directory>src/test/groovy</directory>
                            <directory>src/test</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </testSource>
                    </testSources>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>addTestSources</goal>
                            <goal>generateTestStubs</goal>
                            <goal>compileTests</goal>
                            <goal>removeTestStubs</goal>
                            <goal>removeStubs</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>${junit-platform.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${jupiter.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                        <version>${jupiter.version}</version>
                    </dependency>
                </dependencies>

                <configuration>
                    <detail>true</detail>
                    <argLine>-Xms2g -Xmx2g -XX:MaxPermSize=500m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.region=US</argLine>
                    <includes>
                        <include>%regex[.*]</include>
                    </includes>
                    <excludes>
                        <exclude>%regex[.*Fixme.*]</exclude>
                        <exclude>%regex[.*(Regression|Integration)Test.*]</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.0</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>${junit-platform.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${jupiter.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                        <version>${jupiter.version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <argLine>-Xms2g -Xmx2g -XX:MaxPermSize=500m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.region=US -Duser.timezone=UTC</argLine>
                    <includes>
                        <include>%regex[.*(Regression|Integration)Test.*]</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

所有的功劳都属于桑德斯米-谢谢!!我真是太幸运了,我找到了你的回购!https://github.com/sandersmee/spock-jupiter/blob/master/pom.xml

 类似资料:
  • 我将和作为测试依赖项: 我的集成测试命名正确(在、或之后,默认情况下由Failsafe包含,默认情况下由Surefire排除)。 有什么方法可以使用JUnit5测试和Failsafe吗?

  • 我想在maven测试期间在Java14中运行spock 2.0-M2-Groovy-3.0和junit 5.6.2测试。我的pom看起来: 如果我跑 在maven生命周期中运行spock和junit测试是否可能没有上述警告?

  • 我得堆栈: 想法2019.1.3 Springboot 2.1.6 Java 11 Maven 3.8.0 Groovy 2.5 史巴克1.3 JUnit jupiter 5.5.1 JUnit vintage 5.5.1 GMavenPlus插件2.7.1 我们想开始在Spock测试框架中编写测试。我跟着这个howto,但没有成功。当我尝试运行所有测试时,我的spock测试没有运行。 我能运行一

  • Spock规范如下所示: 是一个测试记录列表,对于该列表的每个成员,方法将按顺序运行。我想要的是,列表的前k个成员运行一个线程,下面的k个成员在第二个线程中运行,等等...理论上,使用Executors框架和类(因为Spock依赖于JUnit),这可以相对容易地完成。 然而,问题是如何将更小的注入到具有方法的类中,因为Spock不允许构造函数,而JUnitCore需要类名(具有测试/规范的类),而

  • 只启动jUnit测试和集成测试。从不执行。 下面是Maven输出的一部分: 我在StackOverflow上发现了几乎相同或相似的问题,但在我的项目中没有一个响应起作用。 null

  • 我遇到了无法使用Maven运行JUnit5测试的问题。在IDE中运行它们工作正常,但使用“mvn测试”会产生以下输出: 这是我的测试课程: pom: 我做了一些研究,我认为这可能与混合JUnit4和JUnit5特性有关,这导致maven surefire插件无法运行测试。然而,我找不到那些剩余的JUnit4特性可能在哪里。我将感谢任何帮助。