groovy / GMavenPlus

A rewrite of GMaven, a Maven plugin for Groovy
Other
285 stars 35 forks source link

How to run groovy test on java code? #69

Closed evbo closed 6 years ago

evbo commented 7 years ago

None of my tests are run. How can I run them? test.groovy is located in submodule/src/test/groovy/package/test.groovy:

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <testSources>
                        <testSource>
                            <directory>${project.basedir}/submodule/src/test/groovy</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </testSource>
                    </testSources>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <version>2.4.7</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

mvn clean test yields no test results:

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ submodule ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.754 s
[INFO] Finished at: 2017-03-14T19:10:53-04:00
[INFO] Final Memory: 18M/212M
[INFO] ------------------------------------------------------------------------
keeganwitt commented 7 years ago

Sorry, I just noticed I hadn't answered this yet. Is your POM from the submodule or the root?

rickle-msft commented 6 years ago

I am also experiencing this issue. I am migrating our tests from Java Junit to Groovy Spock. The tests that are still in Java are compiled and run fine. I believe the reason (the first reason, anyway) the spock tests are not running is because they are not being compiled. The resulting target/test-classes directory only contains class files for the Junit tests.

Here is my pom:

<build>
        <testSourceDirectory>src/test/groovy</testSourceDirectory>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <showWarnings>true</showWarnings>
                        <showDeprecation>true</showDeprecation>
                        <compilerArgument>-Xlint:unchecked</compilerArgument>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                    <configuration>
                        <includes>
                            <include>**/Test*.*</include>
                            <include>**/*Test.*</include>
                            <include>**/*Tests.*</include>
                            <include>**/*Samples*.*</include>
                        </includes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-release-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                </plugin>
                <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.6.1</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>addTestSources</goal>
                                <goal>compile</goal>
                                <goal>generateTestStubs</goal>
                                <goal>compileTests</goal>
                                <goal>removeTestStubs</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <!-- if including source jars, use the no-fork goals
                         otherwise both the Groovy sources and Java stub sources
                         will get included in your jar -->
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <!-- source plugin \> = 2.1 is required to use the no-fork goals -->
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <goals>
                                <goal>jar-no-fork</goal>
                                <goal>test-jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

And here's the debug output for the testCompile goal:

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ azure-storage-blob ---
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-compiler-plugin:3.1, parent: sun.misc.Launcher$AppClassLoader@33909752]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile' with basic configurator -->
[DEBUG]   (f) basedir = C:\Users\frley\Desktop\temp\azure-storage-java
[DEBUG]   (f) buildDirectory = C:\Users\frley\Desktop\temp\azure-storage-java\target
[DEBUG]   (f) classpathElements = [C:\Users\frley\Desktop\temp\azure-storage-java\target\test-classes, C:\Users\frley\Desktop\temp\azure-storage-java\target\classes, C:\Users\frley\.m2\repository\com\microsoft\rest\v2\client-runtime\2.0.0-beta2\client-runtime-2.0.0-beta2.jar, C:\Users\frley\.m2\repository\com\google\guava\guava\20.0\guava-20.0.jar, C:\Users\frley\.m2\repository\io\netty\netty-handler\4.1.25.Final\netty-handler-4.1.25.Final.jar, C:\Users\frley\.m2\repository\io\netty\netty-transport\4.1.25.Final\netty-transport-4.1.25.Final.jar, C:\Users\frley\.m2\repository\io\netty\netty-resolver\4.1.25.Final\netty-resolver-4.1.25.Final.jar, C:\Users\frley\.m2\repository\io\netty\netty-codec\4.1.25.Final\netty-codec-4.1.25.Final.jar, C:\Users\frley\.m2\repository\io\netty\netty-buffer\4.1.25.Final\netty-buffer-4.1.25.Final.jar, C:\Users\frley\.m2\repository\io\netty\netty-common\4.1.25.Final\netty-common-4.1.25.Final.jar, C:\Users\frley\.m2\repository\io\netty\netty-codec-http\4.1.25.Final\netty-codec-http-4.1.25.Final.jar, C:\Users\frley\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.8.11\jackson-datatype-jsr310-2.8.11.jar, C:\Users\frley\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar, C:\Users\frley\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.8.11\jackson-core-2.8.11.jar, C:\Users\frley\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.8.11\jackson-databind-2.8.11.jar, C:\Users\frley\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-xml\2.8.11\jackson-dataformat-xml-2.8.11.jar, C:\Users\frley\.m2\repository\com\fasterxml\jackson\module\jackson-module-jaxb-annotations\2.8.11\jackson-module-jaxb-annotations-2.8.11.jar, C:\Users\frley\.m2\repository\org\codehaus\woodstox\stax2-api\3.1.4\stax2-api-3.1.4.jar, C:\Users\frley\.m2\repository\com\fasterxml\woodstox\woodstox-core\5.0.3\woodstox-core-5.0.3.jar, C:\Users\frley\.m2\repository\io\reactivex\rxjava2\rxjava\2.1.12\rxjava-2.1.12.jar, C:\Users\frley\.m2\repository\org\reactivestreams\reactive-streams\1.0.2\reactive-streams-1.0.2.jar, C:\Users\frley\.m2\repository\org\slf4j\slf4j-api\1.7.22\slf4j-api-1.7.22.jar, C:\Users\frley\.m2\repository\com\microsoft\azure\azure-annotations\1.2.0\azure-annotations-1.2.0.jar, C:\Users\frley\.m2\repository\junit\junit\4.12\junit-4.12.jar, C:\Users\frley\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar, C:\Users\frley\.m2\repository\org\spockframework\spock-core\1.1-groovy-2.4\spock-core-1.1-groovy-2.4.jar, C:\Users\frley\.m2\repository\org\codehaus\groovy\groovy-all\2.4.13\groovy-all-2.4.13.jar]
[DEBUG]   (f) compileSourceRoots = [C:\Users\frley\Desktop\temp\azure-storage-java\src\test\groovy]
[DEBUG]   (f) compilerArgument = -Xlint:unchecked
[DEBUG]   (f) compilerId = javac
[DEBUG]   (f) debug = true
[DEBUG]   (f) encoding = UTF-8
[DEBUG]   (f) failOnError = true
[DEBUG]   (f) forceJavacCompilerUse = false
[DEBUG]   (f) fork = false
[DEBUG]   (f) generatedTestSourcesDirectory = C:\Users\frley\Desktop\temp\azure-storage-java\target\generated-test-sources\test-annotations
[DEBUG]   (f) mojoExecution = org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile {execution: default-testCompile}
[DEBUG]   (f) optimize = false
[DEBUG]   (f) outputDirectory = C:\Users\frley\Desktop\temp\azure-storage-java\target\test-classes
[DEBUG]   (f) showDeprecation = true
[DEBUG]   (f) showWarnings = true
[DEBUG]   (f) skipMultiThreadWarning = false
[DEBUG]   (f) source = 1.8
[DEBUG]   (f) staleMillis = 0
[DEBUG]   (f) target = 1.8
[DEBUG]   (f) useIncrementalCompilation = true
[DEBUG]   (f) verbose = false
[DEBUG]   (f) mavenSession = org.apache.maven.execution.MavenSession@459f7aa3
[DEBUG]   (f) session = org.apache.maven.execution.MavenSession@459f7aa3
[DEBUG] -- end configuration --
[DEBUG] Using compiler 'javac'.
[DEBUG] Source directories: [C:\Users\frley\Desktop\temp\azure-storage-java\src\test\groovy]
[DEBUG] Classpath: [C:\Users\frley\Desktop\temp\azure-storage-java\target\test-classes
 C:\Users\frley\Desktop\temp\azure-storage-java\target\classes
 C:\Users\frley\.m2\repository\com\microsoft\rest\v2\client-runtime\2.0.0-beta2\client-runtime-2.0.0-beta2.jar
 C:\Users\frley\.m2\repository\com\google\guava\guava\20.0\guava-20.0.jar
 C:\Users\frley\.m2\repository\io\netty\netty-handler\4.1.25.Final\netty-handler-4.1.25.Final.jar
 C:\Users\frley\.m2\repository\io\netty\netty-transport\4.1.25.Final\netty-transport-4.1.25.Final.jar
 C:\Users\frley\.m2\repository\io\netty\netty-resolver\4.1.25.Final\netty-resolver-4.1.25.Final.jar
 C:\Users\frley\.m2\repository\io\netty\netty-codec\4.1.25.Final\netty-codec-4.1.25.Final.jar
 C:\Users\frley\.m2\repository\io\netty\netty-buffer\4.1.25.Final\netty-buffer-4.1.25.Final.jar
 C:\Users\frley\.m2\repository\io\netty\netty-common\4.1.25.Final\netty-common-4.1.25.Final.jar
 C:\Users\frley\.m2\repository\io\netty\netty-codec-http\4.1.25.Final\netty-codec-http-4.1.25.Final.jar
 C:\Users\frley\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.8.11\jackson-datatype-jsr310-2.8.11.jar
 C:\Users\frley\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar
 C:\Users\frley\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.8.11\jackson-core-2.8.11.jar
 C:\Users\frley\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.8.11\jackson-databind-2.8.11.jar
 C:\Users\frley\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-xml\2.8.11\jackson-dataformat-xml-2.8.11.jar
 C:\Users\frley\.m2\repository\com\fasterxml\jackson\module\jackson-module-jaxb-annotations\2.8.11\jackson-module-jaxb-annotations-2.8.11.jar
 C:\Users\frley\.m2\repository\org\codehaus\woodstox\stax2-api\3.1.4\stax2-api-3.1.4.jar
 C:\Users\frley\.m2\repository\com\fasterxml\woodstox\woodstox-core\5.0.3\woodstox-core-5.0.3.jar
 C:\Users\frley\.m2\repository\io\reactivex\rxjava2\rxjava\2.1.12\rxjava-2.1.12.jar
 C:\Users\frley\.m2\repository\org\reactivestreams\reactive-streams\1.0.2\reactive-streams-1.0.2.jar
 C:\Users\frley\.m2\repository\org\slf4j\slf4j-api\1.7.22\slf4j-api-1.7.22.jar
 C:\Users\frley\.m2\repository\com\microsoft\azure\azure-annotations\1.2.0\azure-annotations-1.2.0.jar
 C:\Users\frley\.m2\repository\junit\junit\4.12\junit-4.12.jar
 C:\Users\frley\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar
 C:\Users\frley\.m2\repository\org\spockframework\spock-core\1.1-groovy-2.4\spock-core-1.1-groovy-2.4.jar
 C:\Users\frley\.m2\repository\org\codehaus\groovy\groovy-all\2.4.13\groovy-all-2.4.13.jar]
[DEBUG] Output directory: C:\Users\frley\Desktop\temp\azure-storage-java\target\test-classes
[DEBUG] CompilerReuseStrategy: reuseCreated
[DEBUG] useIncrementalCompilation enabled
[DEBUG] Stale source detected: C:\Users\frley\Desktop\temp\azure-storage-java\src\test\groovy\com\microsoft\azure\storage\Samples.java
[DEBUG] Stale source detected: C:\Users\frley\Desktop\temp\azure-storage-java\src\test\groovy\com\microsoft\azure\storage\BlobStorageAPITests.java

You'll notice the last two lines show that only the Java test files are being compiled. Thank you so much for your help!

keeganwitt commented 6 years ago

Your log is showing the Java test files being compiled because that's the log from the Maven Compiler Plugin, not GMavenPlus. Do you have GMavenPlus referenced in a <plugins> element?

As the documentation says

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.

(emphasis mine)

rickle-msft commented 6 years ago

Thanks for getting back so quickly. It is referenced in a plugin tag (see below). I'm not sure why it's not executing, though. I was searching around and said something about GMaven not supporting compiling Groovy files anymore? So I eventually found the Groovy Eclipse plugin, and that seems to be working for me, so I switched over to that for now.

Pom element:

<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.6.1</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>addTestSources</goal>
                                <goal>compile</goal>
                                <goal>generateTestStubs</goal>
                                <goal>compileTests</goal>
                                <goal>removeTestStubs</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
keeganwitt commented 6 years ago

The original GMaven doesn't support compiling Groovy anymore -- GMavenPlus does. You aren't using a custom lifecycle are you?

rickle-msft commented 6 years ago

I see. No, I was just trying to run mvn test. And test-compile while fiddling with settings.

keeganwitt commented 6 years ago

The POM element you copied in your second message looks like the same snippet as in the first message. Just to be clear: You need to have a reference to the plugin in <build><plugins>, not just <build><pluginManagement><plugins>. If you're happy with Groovy-Eclipse, I'm not gonna try to convince you to do otherwise. But if you do want to get GMavenPlus working, I'm happy to continue working with you.

rickle-msft commented 6 years ago

Ohhh. I see. I think I understand now. Sorry, I'm still fairly new to maven. I don't think I want to mess with configurations right now since I have stuff working and I'd rather not disrupt it, but I do very much appreciate your help and insight!

keeganwitt commented 6 years ago

No problem. This is a misunderstanding of how Maven works I frequently see. Basically here's the thing to remember: <build><pluginManagement> specifies default configurations for builds (including of child POMs) to use. But it doesn't add the plugin to any build. <build><plugins> says to actually use the plugin, and include the configuration from pluginManagement, plus you can override optionally override defaults set by pluginManagement.

rickle-msft commented 6 years ago

I see. That makes a lot of sense. I recently flattened the project structure because it wasn't necessary to have a parent-child pom any more. I had a similar issue with dependencies when I didn't realize they had all been left in a dependencyManagement tag. I didn't realize the same could happen for plugins. Thanks again!

keeganwitt commented 6 years ago

Closing for now, as I believe there's nothing further to do.

acidroed commented 2 years ago

Hi @keeganwitt , I have the same issue. I'm not sure that I do everything right. Spock tests are not compiled to target dir. I created a simple multimodule project to test it. Here is my parent pom:

 <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <release>${java.version}</release>
          <source>11</source>
          <target>11</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
      </plugin>

      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
      </plugin>
    </plugins>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0-M5</version>

          <configuration>
            <includes>
              <include>**/*</include>
              <include>**/*Spec.java</include>
              <include>**/*Test.java</include>
            </includes>
          </configuration>
        </plugin>

        <plugin>
          <groupId>org.codehaus.gmavenplus</groupId>
          <artifactId>gmavenplus-plugin</artifactId>
          <version>1.13.0</version>
          <configuration>
            <testSources>
              <testSource>
                <directory>src/test/groovy</directory>
                <includes>
                  <include>**/*</include>
                </includes>
              </testSource>
              <testSource>
                <directory>${project.basedir}/main-module/src/test/groovy</directory>
                <directory>${project.basedir}/src/test/groovy</directory>
                <directory>${project.basedir}/src/test</directory>
                <directory>src/test</directory>
                <includes>
                  <include>**/*</include>
                </includes>
              </testSource>
            </testSources>
          </configuration>
          <executions>
            <execution>
              <goals>
                <goal>compileTests</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

      </plugins>
    </pluginManagement>
  </build>

And the sub module pom:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
      </plugin>

      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.13.0</version>
        <configuration>
          <testSources>
            <testSource>
              <directory>src/test/groovy</directory>
              <includes>
                <include>**/*</include>
              </includes>
            </testSource>
            <testSource>
              <directory>${project.basedir}/main-module/src/test/groovy</directory>
              <directory>${project.basedir}/src/test/groovy</directory>
              <directory>${project.basedir}/src/test</directory>
              <directory>src/test</directory>
              <directory>src/test/groovy/</directory>
              <includes>
                <include>**/*</include>
              </includes>
            </testSource>
          </testSources>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compileTests</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
keeganwitt commented 2 years ago

I suspect your directory structure doesn't match the sources you've configured. Without knowing your directory layout, I can't tell you what you should configure instead. I do think it's odd that you have a main-module in your subproject pom. Can you maybe run tree in this test project so I can understand the layout?