Maven - 运行 java 和 groovy 测试

问题描述 投票:0回答:1

跑步时

mvn test
仅运行 java 测试

我想学习如何在没有 IDE 的情况下运行所有测试

输出(重要部分)

[INFO] --- gmavenplus-plugin:3.0.0:addTestSources (default) @ project ---

[INFO] --- swagger-codegen-maven-plugin:3.0.35:generate (generate-orchestrator) @ project ---
...
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 32 source files to ...project/target/classes
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory ...project/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to ...project/target/test-classes
[INFO] 
[INFO] --- gmavenplus-plugin:3.0.0:compileTests (default) @ project ---
[INFO] Using isolated classloader, without GMavenPlus classpath.
[INFO] Using Groovy 3.0.13 to perform compileTests.
[INFO] Parallel parsing disabled.
[INFO] Compiled 8 files.
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ project ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.258 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

测试树(匿名,但想法是存在的)

src/test/
├── groovy
│   └── com
│             └── project
│                   ├── ASampleTest.groovy
│                   ├── smoke
│                   │   ├── BlaTest.groovy
│                   │   └── utils
│                   │       └── Provider.groovy
│                   └── unit
│                       ├── BlaTest2.groovy
│                       └── BlaTest.groovy
├── java
│   └── com
│               └── project
│                   └── AppTests.java

我知道只有 java 测试运行,因为 AppTests.java 没有测试:

@SpringBootTest
class AppTests {

    @Test
    void contextLoads() {
    }

}

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project</name>

    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>2.2-M3-groovy-3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-spring</artifactId>
            <version>2.2-M3-groovy-3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>3.0.13</version>
            <type>pom</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>io.swagger.codegen.v3</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>3.0.35</version>
                <executions>
                    <execution>
                        <id>generate-orchestrator</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/openapi/geo_search_v4.yaml</inputSpec>
                            <language>java</language>
                            <modelPackage>com.sabre.zjwpit.geo.api</modelPackage>
                            <generateModelTests>false</generateModelTests>
                            <generateApis>false</generateApis>
                            <generateApiTests>false</generateApiTests>
                            <library>resttemplate</library>
                            <configOptions>
                                <dateLibrary>java8-localdatetime</dateLibrary>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.yaml</groupId>
                        <artifactId>snakeyaml</artifactId>
                        <version>1.30</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>addTestSources</goal>
                            <goal>compileTests</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-Dplatform.environment=ci</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是我刚刚克隆的一个项目,我在 IntelliJ Run All 测试中尝试过,但它也不起作用。我可以使用 IntelliJ 运行所有常规测试。但我有兴趣从命令行运行所有测试,主要是为了学习目的。

java maven groovy command-line-interface spock
1个回答
0
投票

您的有效 POM 显示您默认使用 Surefire 2.22.2。如果您升级到更新的版本,它应该可以工作:

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.1.0</version>
  <configuration>
    <argLine>-Dplatform.environment=ci</argLine>
  </configuration>
</plugin>

如果现在您的 Spock 测试正在运行,但 Spring Boot 测试尚未运行,则可能您导入了 JUnit 4 类

org.junit.Test
,它也在您的类路径中。相反,对于 JUnit 5,您需要
org.junit.jupiter.api.Test

package com.project;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AppTests {
  @Test // Do *not* import org.junit.Test!
  void contextLoads() {}
}

现在,Java JUnit Jupiter 测试和 Spock 2 测试应该在同一平台上运行,即 JUnit 5。

© www.soinside.com 2019 - 2024. All rights reserved.