You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2020/05/05 22:34:20 UTC

[maven-surefire] 01/01: Documentation and IT for JUnit5 and Spock

This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch junit5-spock
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit 5a56282add406a271e18019f51d6572450c2e17f
Author: tibordigana <ti...@apache.org>
AuthorDate: Wed May 6 00:34:11 2020 +0200

    Documentation and IT for JUnit5 and Spock
---
 .../src/site/apt/examples/spock.apt.vm             | 174 +++++++++++++++++++++
 maven-surefire-plugin/src/site/site.xml            |   1 +
 .../apache/maven/surefire/its/jiras/SpockIT.java   |  38 +++++
 .../src/test/resources/junit5-spock/pom.xml        |  70 +++++++++
 .../junit5-spock/src/main/java/pkg/Calculator.java |  28 ++++
 .../src/test/groovy/pkg/CalculatorTest.groovy      |  26 +++
 6 files changed, 337 insertions(+)

diff --git a/maven-surefire-plugin/src/site/apt/examples/spock.apt.vm b/maven-surefire-plugin/src/site/apt/examples/spock.apt.vm
new file mode 100644
index 0000000..09e6b2c
--- /dev/null
+++ b/maven-surefire-plugin/src/site/apt/examples/spock.apt.vm
@@ -0,0 +1,174 @@
+ ------
+ Using Spock
+ ------
+ Tibor Digana <ti...@apache.org>
+ ------
+ 2020-05-05
+ ------
+
+ ~~ Licensed to the Apache Software Foundation (ASF) under one
+ ~~ or more contributor license agreements.  See the NOTICE file
+ ~~ distributed with this work for additional information
+ ~~ regarding copyright ownership.  The ASF licenses this file
+ ~~ to you under the Apache License, Version 2.0 (the
+ ~~ "License"); you may not use this file except in compliance
+ ~~ with the License.  You may obtain a copy of the License at
+ ~~
+ ~~   http://www.apache.org/licenses/LICENSE-2.0
+ ~~
+ ~~ Unless required by applicable law or agreed to in writing,
+ ~~ software distributed under the License is distributed on an
+ ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~~ KIND, either express or implied.  See the License for the
+ ~~ specific language governing permissions and limitations
+ ~~ under the License.
+
+ ~~ NOTE: For help with the syntax of this file, see:
+ ~~ http://maven.apache.org/doxia/references/apt-format.html
+
+Using the Spock based on JUnit5 engine
+
+* Sample project with Spock/Groovy and JUnit5
+
+  This {{{https://github.com/apache/maven-surefire/tree/master/surefire-its/src/test/resources/junit5-spock}sample code}}
+  shows you how the ${thisPlugin} plugin works with Spock/Groovy and JUnit5. The project contains two dependencies
+  <<<spock-core>>> and <<<junit-jupiter-engine>>>, and Groovy compiler <<<gmavenplus-plugin>>>.
+
+  You can create tests in test source directory for Groovy (i.e., <<<src/test/groovy>>>). This is an example with
+  parameterized test which is executed twice.
+
+
++---+
+import pkg.Calculator
+import spock.lang.Specification
+
+class CalculatorTest extends Specification
+{
+    def "Multiply: #a * #b = #expectedResult"()
+    {
+        given: "Calculator"
+        def calc = new Calculator()
+
+        when: "multiply"
+        def result = calc.multiply( a, b )
+
+        then: "result is as expected"
+        result == expectedResult
+        println "result = ${result}"
+
+        where:
+        a  | b | expectedResult
+        1  | 2 | 3
+        -5 | 2 | -3
+    }
+}
++---+
+
+
+  If you want to additionally mix Spock tests with the JUnit4, you should add the JUnit Vintage Engine
+  in the test dependencies.
+
+
+#{if}(${project.artifactId}=="maven-failsafe-plugin")
++---+
+<dependencies>
+    [...]
+    <dependency>
+        <groupId>org.spockframework</groupId>
+        <artifactId>spock-core</artifactId>
+        <version>2.0-M2-groovy-3.0</version>
+        <scope>test</scope>
+    </dependency>
+    <dependency>
+        <groupId>org.junit.jupiter</groupId>
+        <artifactId>junit-jupiter-engine</artifactId>
+        <version>5.6.2</version>
+        <scope>test</scope>
+    </dependency>
+    [...]
+</dependencies>
+...
+<build>
+    <plugins>
+        [...]
+        <plugin>
+            <groupId>org.codehaus.gmavenplus</groupId>
+            <artifactId>gmavenplus-plugin</artifactId>
+            <version>1.9.0</version>
+            <executions>
+                <execution>
+                    <goals>
+                        <goal>compileTests</goal>
+                    </goals>
+                </execution>
+            </executions>
+        </plugin>
+        <plugin>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>${project.artifactId}</artifactId>
+            <version>${project.version}</version>
+            <executions>
+                <execution>
+                    <id>integration-test</id>
+                    <goals>
+                        <goal>integration-test</goal>
+                    </goals>
+                </execution>
+                <execution>
+                    <id>verify</id>
+                    <goals>
+                        <goal>verify</goal>
+                    </goals>
+                </execution>
+            </executions>
+        </plugin>
+        [...]
+    </plugins>
+</build>
+...
++---+
+#{else}
++---+
+<dependencies>
+    [...]
+    <dependency>
+        <groupId>org.spockframework</groupId>
+        <artifactId>spock-core</artifactId>
+        <version>2.0-M2-groovy-3.0</version>
+        <scope>test</scope>
+    </dependency>
+    <dependency>
+        <groupId>org.junit.jupiter</groupId>
+        <artifactId>junit-jupiter-engine</artifactId>
+        <version>5.6.2</version>
+        <scope>test</scope>
+    </dependency>
+    [...]
+</dependencies>
+...
+<build>
+    <plugins>
+        [...]
+        <plugin>
+            <groupId>org.codehaus.gmavenplus</groupId>
+            <artifactId>gmavenplus-plugin</artifactId>
+            <version>1.9.0</version>
+            <executions>
+                <execution>
+                    <goals>
+                        <goal>compileTests</goal>
+                    </goals>
+                </execution>
+            </executions>
+        </plugin>
+        <plugin>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>${project.artifactId}</artifactId>
+            <version>${project.version}</version>
+        </plugin>
+        [...]
+    </plugins>
+</build>
+...
++---+
+#{end}
diff --git a/maven-surefire-plugin/src/site/site.xml b/maven-surefire-plugin/src/site/site.xml
index cbee436..b1f722c 100644
--- a/maven-surefire-plugin/src/site/site.xml
+++ b/maven-surefire-plugin/src/site/site.xml
@@ -43,6 +43,7 @@
       <item name="Using JUnit" href="examples/junit.html"/>
       <item name="Using JUnit 5 Platform" href="examples/junit-platform.html"/>
       <item name="Using POJO Tests" href="examples/pojo-test.html"/>
+      <item name="Using Spock" href="examples/spock.html"/>
       <item name="Using Cucumber" href="examples/cucumber.html"/>
       <item name="Special characters in directories" href="examples/cwd.html"/>
       <item name="Skipping Tests" href="examples/skipping-tests.html"/>
diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/SpockIT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/SpockIT.java
new file mode 100644
index 0000000..2f354cb
--- /dev/null
+++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/SpockIT.java
@@ -0,0 +1,38 @@
+package org.apache.maven.surefire.its.jiras;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class SpockIT extends SurefireJUnit4IntegrationTestCase
+{
+
+    @Test
+    public void test()
+    {
+        unpack( "junit5-spock" )
+            .executeTest()
+            .verifyErrorFree( 2 );
+    }
+}
diff --git a/surefire-its/src/test/resources/junit5-spock/pom.xml b/surefire-its/src/test/resources/junit5-spock/pom.xml
new file mode 100644
index 0000000..feb7e1d
--- /dev/null
+++ b/surefire-its/src/test/resources/junit5-spock/pom.xml
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+
+<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/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.apache.maven.plugins.surefire</groupId>
+    <artifactId>junit5-spock</artifactId>
+    <version>1.0.0</version>
+
+    <properties>
+        <maven.compiler.source>${java.specification.version}</maven.compiler.source>
+        <maven.compiler.target>${java.specification.version}</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.spockframework</groupId>
+            <artifactId>spock-core</artifactId>
+            <version>2.0-M2-groovy-3.0</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-engine</artifactId>
+            <version>5.6.2</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.gmavenplus</groupId>
+                <artifactId>gmavenplus-plugin</artifactId>
+                <version>1.9.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>compileTests</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <version>${surefire.version}</version>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/surefire-its/src/test/resources/junit5-spock/src/main/java/pkg/Calculator.java b/surefire-its/src/test/resources/junit5-spock/src/main/java/pkg/Calculator.java
new file mode 100644
index 0000000..8886bd5
--- /dev/null
+++ b/surefire-its/src/test/resources/junit5-spock/src/main/java/pkg/Calculator.java
@@ -0,0 +1,28 @@
+package pkg;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public class Calculator
+{
+    public int sum( int x, int y )
+    {
+        return x + y;
+    }
+}
diff --git a/surefire-its/src/test/resources/junit5-spock/src/test/groovy/pkg/CalculatorTest.groovy b/surefire-its/src/test/resources/junit5-spock/src/test/groovy/pkg/CalculatorTest.groovy
new file mode 100644
index 0000000..163398e
--- /dev/null
+++ b/surefire-its/src/test/resources/junit5-spock/src/test/groovy/pkg/CalculatorTest.groovy
@@ -0,0 +1,26 @@
+package pkg
+
+import pkg.Calculator
+import spock.lang.Specification
+
+class CalculatorTest extends Specification
+{
+    def "Multiply: #a * #b = #expectedResult"()
+    {
+
+        given: "Calculator"
+        def calc = new Calculator()
+
+        when: "multiply"
+        def result = calc.multiply( a, b )
+
+        then: "result is as expected"
+        result == expectedResult
+        println "result = ${result}"
+
+        where:
+        a  | b | expectedResult
+        1  | 2 | 3
+        -5 | 2 | -3
+    }
+}