Maven -运行java和groovy测试

ercv8c1e  于 4个月前  发布在  Maven
关注(0)|答案(1)|浏览(59)

运行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运行所有测试,它也不工作。我可以运行所有的groovy测试与IntelliJ。但我感兴趣的是运行所有的测试从命令行主要是为了学习的目的。

ncecgwcz

ncecgwcz1#

您的有效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测试正在运行,但Sping 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。

**更新:**JUnit 4位于类路径中的原因是您通过groovy-all获得了它,它还拉入了groovy-test。也许下次要么只依赖于groovy +您真正需要的其他模块,要么显式排除groovy-test。然后IDE将不再提供导入错误的annotation。但它仍然会提供TestNG @Test annotation,因此如果您不需要它,也许您也想排除它。

<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>3.0.18</version>
  <type>pom</type>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-test</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-testng</artifactId>
    </exclusion>
  </exclusions>
</dependency>

相关问题