【问题标题】:Running Spock unit test along with TestNG in Spring boot maven based project在基于 Spring Boot maven 的项目中运行 Spock 单元测试和 TestNG
【发布时间】:2017-05-06 18:05:44
【问题描述】:

我正在使用 IntelliJ、Spring Boot 1.3、java8 和 maven。

我们在我们的单元中使用了 TestNG,但是当我开始使用 Spock 进行单元测试时,我真的很喜欢在 Groovy 中使用 Spock 编写测试。这是文件夹结构:

src->test->java -> 所有基于 java 的 TestNG 测试都在这里
src->test->groovy -> 所有基于 groovy 的 Spock 测试都在这里。所有测试都有一个 Spec 后缀,这就是我配置surefire插件来查看这些测试的方式。
现在,当我运行这些单独的测试时,它们工作得很好。但是当我运行像mvn testmvn clean install 这样的maven 生命周期命令时,即使我启用了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>    

我想我在插件中遗漏了一些东西,它可以让 maven 在 maven 生命周期命令期间与 TestNG 一起运行 Spock 测试。我想我在这里遗漏了一些琐碎的事情。有人可以在这里给我一些关于我应该在 pom 中添加什么的指示,或者如果有人在 github 中有一个我可以查看的示例骨架项目。谢谢。

【问题讨论】:

标签: maven testng spock maven-surefire-plugin


【解决方案1】:

我能够得到这个工作。这是我更新的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>

我希望这对尝试一起运行 TestNG 和 Spock 测试的其他人有所帮助。

【讨论】:

    猜你喜欢
    • 2020-04-17
    • 2014-09-30
    • 2017-07-15
    • 2018-12-18
    • 2019-05-05
    • 1970-01-01
    • 2021-07-23
    • 2013-03-12
    • 2014-10-25
    相关资源
    最近更新 更多