Search code examples
javamavengroovycommand-line-interfacespock

Maven - run java and groovy tests


When running mvn test Only java tests are run

I want to learn how to run all tests without IDE

Output (important part)

[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] ------------------------------------------------------------------------

tests tree (anonimized, but idea is there)

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

And I know that only java tests run because AppTests.java has no tests:

@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>

This is a project I got and just cloned, I've tried in IntelliJ Run All tests and it doesn't work either. I can run all groovy test with IntelliJ. but I'm interested in running all tests from command line mainly for learning puposes.


Solution

  • Your effective POM shows that you use Surefire 2.22.2 by default. If you upgrade to a more recent version, it should work:

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

    If now your Spock tests are running, but not your Spring Boot test, probably you imported the JUnit 4 class org.junit.Test, which is also on your classpath. Instead, you need org.junit.jupiter.api.Test for JUnit 5:

    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() {}
    }
    

    Now, both the Java JUnit Jupiter tests and the Spock 2 tests should run on the same platform, i.e. JUnit 5.


    Update: The reason that JUnit 4 is on your classpath is that you got it via groovy-all, which also pulls in groovy-test. Maybe next time either only depend on groovy + other modules that you really need, or explicitly exclude groovy-test. Then the IDE will no longer offer to import the wrong annotation. But it will still offer the TestNG @Test annotation, so maybe you want to exclude that, too, if you do not need it.

    <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>