在Maven生命周期中运行JUnit5和Spock。

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

Run JUnit5 and Spock in maven lifecycle

问题

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.3.2</version>
                </dependency>
            </dependencies>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                    <include>**/*Spec.java</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

Please note that the warning message you mentioned is indicating that the junit-platform-surefire-provider is deprecated and you should use the built-in support in Maven Surefire version 2.22.0 or higher. However, as of my last knowledge update in September 2021, if you're using Maven Surefire 2.22.0 or higher, it should be used automatically without the need for the deprecated provider.

You might want to make sure you're using an appropriate version of Maven Surefire that supports both Spock and JUnit 5. Also, keep in mind that there might have been updates or changes after my last knowledge update, so it's a good idea to consult the official documentation or community resources for the most up-to-date information.

英文:

I want to run spock 2.0-M2-groovy-3.0 and junit 5.6.2 test in java 14 during maven test.
My pom looks:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.0.RELEASE&lt;/version&gt;
&lt;relativePath/&gt;
&lt;/parent&gt;
&lt;groupId&gt;test&lt;/groupId&gt;
&lt;artifactId&gt;test&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;properties&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
&lt;java.version&gt;14&lt;/java.version&gt;
&lt;guava-version&gt;29.0-jre&lt;/guava-version&gt;
&lt;spock-version&gt;2.0-M2-groovy-3.0&lt;/spock-version&gt;
&lt;spring-boot-admin-version&gt;2.2.3&lt;/spring-boot-admin-version&gt;
&lt;sonar-maven-plugin.version&gt;3.7.0.1746&lt;/sonar-maven-plugin.version&gt;
&lt;maven-surefire-plugin.version&gt;2.22.0&lt;/maven-surefire-plugin.version&gt;
&lt;jacoco.version&gt;0.8.5&lt;/jacoco.version&gt;
&lt;sonar.java.coveragePlugin&gt;jacoco&lt;/sonar.java.coveragePlugin&gt;
&lt;sonar.dynamicAnalysis&gt;reuseReports&lt;/sonar.dynamicAnalysis&gt;
&lt;sonar.jacoco.reportPath&gt;${project.basedir}/../target/jacoco.exec&lt;/sonar.jacoco.reportPath&gt;
&lt;sonar.language&gt;java&lt;/sonar.language&gt;
&lt;maven.test.skip&gt;false&lt;/maven.test.skip&gt;
&lt;junit-platform.version&gt;1.6.2&lt;/junit-platform.version&gt;
&lt;jupiter.version&gt;5.6.2&lt;/jupiter.version&gt;
&lt;spock.version&gt;2.0-M2-groovy-3.0&lt;/spock.version&gt;
&lt;gmavenplus-plugin.version&gt;1.9.0&lt;/gmavenplus-plugin.version&gt;
&lt;maven-failsafe-plugin.version&gt;2.22.0&lt;/maven-failsafe-plugin.version&gt;
&lt;groovy.version&gt;3.0.3&lt;/groovy.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.google.guava&lt;/groupId&gt;
&lt;artifactId&gt;guava&lt;/artifactId&gt;
&lt;version&gt;${guava-version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;de.codecentric&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-admin-starter-client&lt;/artifactId&gt;
&lt;version&gt;${spring-boot-admin-version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.codehaus.groovy&lt;/groupId&gt;
&lt;artifactId&gt;groovy-all&lt;/artifactId&gt;
&lt;version&gt;${groovy.version}&lt;/version&gt;
&lt;type&gt;pom&lt;/type&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.spockframework&lt;/groupId&gt;
&lt;artifactId&gt;spock-spring&lt;/artifactId&gt;
&lt;version&gt;${spock.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.spockframework&lt;/groupId&gt;
&lt;artifactId&gt;spock-core&lt;/artifactId&gt;
&lt;version&gt;${spock.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
&lt;version&gt;${jupiter.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
&lt;version&gt;${jupiter.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;version&gt;${jupiter.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.codehaus.gmavenplus&lt;/groupId&gt;
&lt;artifactId&gt;gmavenplus-plugin&lt;/artifactId&gt;
&lt;version&gt;${gmavenplus-plugin.version}&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;goals&gt;
&lt;goal&gt;addSources&lt;/goal&gt;
&lt;goal&gt;addTestSources&lt;/goal&gt;
&lt;goal&gt;compile&lt;/goal&gt;
&lt;goal&gt;compileTests&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;configuration&gt;
&lt;testSources&gt;
&lt;testSource&gt;
&lt;directory&gt;${project.basedir}/src/test/groovy&lt;/directory&gt;
&lt;includes&gt;
&lt;include&gt;**/*.groovy&lt;/include&gt;
&lt;/includes&gt;
&lt;/testSource&gt;
&lt;/testSources&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;includes&gt;
&lt;include&gt;**/*Test.java&lt;/include&gt;
&lt;include&gt;**/*Spec.java&lt;/include&gt;
&lt;/includes&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.jacoco&lt;/groupId&gt;
&lt;artifactId&gt;jacoco-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;${jacoco.version}&lt;/version&gt;
&lt;configuration&gt;
&lt;skip&gt;${maven.test.skip}&lt;/skip&gt;
&lt;destFile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/destFile&gt;
&lt;dataFile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/dataFile&gt;
&lt;output&gt;file&lt;/output&gt;
&lt;append&gt;true&lt;/append&gt;
&lt;excludes&gt;
&lt;exclude&gt;*MethodAccess&lt;/exclude&gt;
&lt;/excludes&gt;
&lt;/configuration&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;id&gt;jacoco-initialize&lt;/id&gt;
&lt;goals&gt;
&lt;goal&gt;prepare-agent&lt;/goal&gt;
&lt;/goals&gt;
&lt;phase&gt;test-compile&lt;/phase&gt;
&lt;/execution&gt;
&lt;execution&gt;
&lt;id&gt;jacoco-site&lt;/id&gt;
&lt;phase&gt;verify&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;report&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;pluginManagement&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.sonarsource.scanner.maven&lt;/groupId&gt;
&lt;artifactId&gt;sonar-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;${sonar-maven-plugin.version}&lt;/version&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/pluginManagement&gt;
&lt;/build&gt;
&lt;/project&gt;

and if I run

mvn clean install

only JUnit test is running, but if I change maven-surefire-plugin like:

 &lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.platform&lt;/groupId&gt;
&lt;artifactId&gt;junit-platform-surefire-provider&lt;/artifactId&gt;
&lt;version&gt;1.3.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;configuration&gt;
&lt;includes&gt;
&lt;include&gt;**/*Test.java&lt;/include&gt;
&lt;include&gt;**/*Spec.java&lt;/include&gt;
&lt;/includes&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;

spock and junit test are running together but I get warning:

 +-------------------------------------------------------------------------------+
| WARNING:                                                                      |
| The junit-platform-surefire-provider has been deprecated and is scheduled to  |
| be removed in JUnit Platform 1.4. Please use the built-in support in Maven    |
| Surefire &gt;= 2.22.0 instead.                                                   |
| &#187; https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven |
+-------------------------------------------------------------------------------+

Is it possible achive running spock and junit test during maven lifecycle without above warning?

答案1

得分: 3

以下是翻译后的内容:

首先,将 maven-surefire-plugin 升级到 3.0.0(-M4) 版本,该版本默认运行 JUnit Platform(JUnit 5)测试。然后,只需添加 spock-core 2.0 依赖即可。从我的博客文章中可以找到一个示例工程,该工程展示了关于 Spock 1 到 2 迁移的内容(当然推荐使用 Groovy 3.0)。

<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>info.solidsoft.blog.spock2-migration</groupId>
    <artifactId>spock2-migration-maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spock.version>2.0-M2-groovy-2.5</spock.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.8.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compileTests</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <version>2.5.11</version>
                        <scope>runtime</scope>
                        <type>pom</type>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
                <configuration>
                    <includes>
                        <include>**/*Spec.java</include> <!-- Yes, .java extension -->
                        <include>**/*Test.java</include> <!-- Just in case of having also "normal" JUnit tests -->
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>${spock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <!-- Required only to use JUnit 4 @Rule/@ClassRule -->
            <groupId>org.spockframework</groupId>
            <artifactId>spock-junit4</artifactId>
            <version>${spock.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
英文:

First, upgrade maven-surefire-plugin to 3.0.0(-M4) which runs JUnit Platform (JUnit 5) tests by default. Then it should be enough to just add spock-core 2.0 dependency. Sample working project from my blog post treating about Spock 1 -> 2 migration (of course Groovy 3.0 is recommended to use).

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;info.solidsoft.blog.spock2-migration&lt;/groupId&gt;
&lt;artifactId&gt;spock2-migration-maven&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;
&lt;properties&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
&lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
&lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
&lt;spock.version&gt;2.0-M2-groovy-2.5&lt;/spock.version&gt;
&lt;/properties&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.codehaus.gmavenplus&lt;/groupId&gt;
&lt;artifactId&gt;gmavenplus-plugin&lt;/artifactId&gt;
&lt;version&gt;1.8.1&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;goals&gt;
&lt;goal&gt;compile&lt;/goal&gt;
&lt;goal&gt;compileTests&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.codehaus.groovy&lt;/groupId&gt;
&lt;artifactId&gt;groovy-all&lt;/artifactId&gt;
&lt;version&gt;2.5.11&lt;/version&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;type&gt;pom&lt;/type&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;version&gt;3.0.0-M4&lt;/version&gt;
&lt;configuration&gt;
&lt;includes&gt;
&lt;include&gt;**/*Spec.java&lt;/include&gt; &lt;!-- Yes, .java extension --&gt;
&lt;include&gt;**/*Test.java&lt;/include&gt; &lt;!-- Just in case of having also &quot;normal&quot; JUnit tests --&gt;
&lt;/includes&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.spockframework&lt;/groupId&gt;
&lt;artifactId&gt;spock-core&lt;/artifactId&gt;
&lt;version&gt;${spock.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;!-- Required only to use JUnit 4 @Rule/@ClassRule --&gt;
&lt;groupId&gt;org.spockframework&lt;/groupId&gt;
&lt;artifactId&gt;spock-junit4&lt;/artifactId&gt;
&lt;version&gt;${spock.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/project&gt;

huangapple
  • 本文由 发表于 2020年5月19日 18:25:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61888746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定