使用maven-surefire-plugin测试JUnit和Spock

kq4fsx7k  于 2023-10-20  发布在  Maven
关注(0)|答案(3)|浏览(191)

我在src/test/java/中有JUnit测试,在src/test/groovy/中有Groovy测试。我可以在IDE中运行所有测试,但我想将它们添加到mvn test
如果我用途:

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M5</version>
      <configuration>
      <argLine>${arg}</argLine>
      <includes>
          <include>**/*Test.class</include>
      </includes>
</plugin>

mvn test运行所有从groovy包,但如果我添加junit为surefire像这样:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>3.0.0-M5</version>
   <configuration>
      <argLine>${arg}</argLine>
      <includes>
         <include>**/*Test.class</include>
      </includes>
   </configuration>
   <dependencies>
      <dependency>
         <groupId>org.apache.maven.surefire</groupId>
         <artifactId>surefire-junit47</artifactId>
         <version>3.0.0-M5</version>
      </dependency>
   </dependencies>
</plugin>

mvn test从java包运行所有。我已经尝试了很多方法,但我无法将maven配置为在一个构建中同时运行这两种方法。怎么可能呢?

zqdjd7g9

zqdjd7g91#

请参阅Spock-Example项目,了解如何正确配置maven构建以运行Spock 2.x。

<?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>org.spockframework</groupId>
  <artifactId>spock-example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>Spock Framework - Example Project</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <groovy.version>3.0.9</groovy.version>
  </properties>

  <build>
    <plugins>
      <!-- Mandatory plugins for using Spock -->
      <plugin>
        <!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,
        visit https://github.com/groovy/GMavenPlus/wiki -->
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.12.0</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>compileTests</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <configuration>
          <useModulePath>false</useModulePath> <!-- https://issues.apache.org/jira/browse/SUREFIRE-1809 -->
          <useFile>false</useFile>
          <includes>
            <include>**/*Test</include>
            <include>**/*Spec</include>
          </includes>
          <statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
            <disable>false</disable>
            <version>3.0</version>
            <usePhrasedFileName>false</usePhrasedFileName>
            <usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
            <usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
            <usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
          </statelessTestsetReporter>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-bom</artifactId>
        <version>2.0-groovy-3.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <!-- Mandatory dependencies for using Spock -->
    <dependency>
      <groupId>org.spockframework</groupId>
      <artifactId>spock-core</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- Optional dependencies for using Spock -->
    <dependency> <!-- use a specific Groovy version rather than the one specified by spock-core -->
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy</artifactId>
      <version>${groovy.version}</version>
    </dependency>
    <dependency> <!-- enables mocking of classes (in addition to interfaces) -->
      <groupId>net.bytebuddy</groupId>
      <artifactId>byte-buddy</artifactId>
      <version>1.11.0</version>
      <scope>test</scope>
    </dependency>
    <dependency> <!-- enables mocking of classes without default constructor (together with ByteBuddy or CGLIB) -->
      <groupId>org.objenesis</groupId>
      <artifactId>objenesis</artifactId>
      <version>3.2</version>
      <scope>test</scope>
    </dependency>
    <dependency> <!-- only required if Hamcrest matchers are used -->
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>2.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
xv8emn3q

xv8emn3q2#

我不得不使用junit-vintage-engine而不是surefire-junit-47

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>3.0.0-M5</version>
   <configuration>
      <argLine>${arg}</argLine>
      <includes>
         <include>**/*Test.class</include>
      </includes>
   </configuration>
   <dependencies>
      <dependency>
         <groupId>org.junit.vintage</groupId>
         <artifactId>junit-vintage-engine</artifactId>
         <version>5.8.2</version>
      </dependency>
   </dependencies>
</plugin>
cpjpxq1n

cpjpxq1n3#

在Junit5上使用Spock测试没有检测到任何在>= 3.0.0-M5上运行的测试,而且Maven日志显示Surefire选择了JUnit4Provider而不是任何JUnit5提供程序。
surefire-junit-platform添加到插件依赖项对我来说很有效:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
    <dependencies>
        <dependency>
            <!-- Required to detect tests on version > 3.0.0-M4 -->
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit-platform</artifactId>
            <version>${maven-surefire-plugin.version}</version>
        </dependency>
    </dependencies>
</plugin>

相关问题