在基于Spring boot maven的项目中运行Spock单元测试和TestNG

在基于Spring boot maven的项目中运行Spock单元测试和TestNG,maven,testng,spock,maven-surefire-plugin,Maven,Testng,Spock,Maven Surefire Plugin,我正在使用IntelliJ、SpringBoot1.3、java8和maven 我们在单元中使用TestNG,但当我开始使用Spock进行单元测试时,我非常喜欢在Groovy中使用Spock编写测试。以下是文件夹结构: src->test->java->所有基于java的TestNG测试都在这里 src->test->groovy->所有基于groovy的Spock测试都在这里。所有测试都有一个Spec后缀,这就是我配置surefire插件来查看这些测试的方式。 现在,当我运行这些单独的测试时,

我正在使用IntelliJ、SpringBoot1.3、java8和maven

我们在单元中使用TestNG,但当我开始使用Spock进行单元测试时,我非常喜欢在Groovy中使用Spock编写测试。以下是文件夹结构:

src->test->java->所有基于java的TestNG测试都在这里
src->test->groovy->所有基于groovy的Spock测试都在这里。所有测试都有一个Spec后缀,这就是我配置surefire插件来查看这些测试的方式。
现在,当我运行这些单独的测试时,它们工作得很好。但当我运行maven lifecycle命令,如
mvn test
mvn clean install
,即使启用了maven编译器和相应的库,Spock测试也不会运行

以下是我的pom的外观:

 <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${version.surefire}</version>
                <configuration>
                    <skipTests>false</skipTests>
                    <useFile>false</useFile>
                    <includes>
                        <include>**/*Spec.*</include>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>

            </plugin>

            <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>1.0-groovy-2.4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency> <!-- enables mocking of classes without default
        constructor (together with CGLIB) -->
        <groupId>org.objenesis</groupId>
        <artifactId>objenesis</artifactId>
        <version>2.1</version>
        <scope>test</scope>
    </dependency>    

org.codehaus.gmavenplus
gmavenplus插件
1.5
编译
测试编译
org.apache.maven.plugins
maven surefire插件
${version.surefire}
假的
假的
**/*规格*
**/*Test.java
org.spockframework
斯波克堆芯
1.0-groovy-2.4
测试
cglib
cglibnodep
释放
测试
org.objenesis
正视
2.1
测试

我想我在插件中缺少了一些东西,可以让maven在maven生命周期命令期间运行Spock测试和TestNG。我想我错过了一件小事。有人能在这里给我一些建议,我应该在pom中添加什么,或者如果有人在github中有一个示例框架项目,我可以看看。谢谢

我能让它工作起来。这是我更新的pom

我有两个问题:

-作为maven测试生命周期的一部分运行TestNG和Groovy测试。这是通过在surefire插件中添加依赖项实现的。见下文pom

-在修复了这个问题之后,Intellij出现了另一个问题,它抱怨基于groovy的测试中已经存在类。这是因为在目标文件夹中生成了存根。为此,我必须按照下面链接的建议提供覆盖这些目录的配置

<plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>addTestSources</goal>
                        <goal>testGenerateStubs</goal>
                        <goal>testCompile</goal>
                        <goal>removeTestStubs</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!--<This setting is for intellij bug. This override default location of groovy stubs. for more info
                check this : https://youtrack.jetbrains.com/issue/IDEA-153779>-->
                <stubsOutputDirectory>${project.build.directory}/generated-groovy-stubs</stubsOutputDirectory>
                <testStubsOutputDirectory>${project.build.directory}/generated-groovy-test-stubs</testStubsOutputDirectory>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${version.surefire}</version>
            <configuration>
                <includes>
                    <include>**/*Spec*.groovy</include>
                    <include>**/*Test*.java</include>
                </includes>
                <properties>
                    <property>
                        <name>junit</name>
                        <value>false</value>
                    </property>
                </properties>
                <threadCount>1</threadCount>
            </configuration>
            <!--Below dependency let's surefire play nice with groovy test and testng tests-->
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>${version.surefire}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-testng</artifactId>
                    <version>${version.surefire}</version>
                </dependency>
            </dependencies>
        </plugin>

org.codehaus.gmavenplus
gmavenplus插件
1.5
addTestSources
TestGenerateSubs
测试编译
除去树桩
${project.build.directory}/生成的groovy存根
${project.build.directory}/生成的groovy测试存根
org.apache.maven.plugins
maven surefire插件
${version.surefire}
**/*Spec*.groovy
**/*Test*.java
朱尼特
假的
1.
org.apache.maven.surefire
surefire-junit47
${version.surefire}
org.apache.maven.surefire
surefire测试
${version.surefire}

我希望这能帮助其他试图一起运行TestNG和Spock测试的人。

您使用的是什么
***Spec.java
就像示例中的: