How to add Spock tests to existing Maven build with Java unit tests?

2266 views
Skip to first unread message

David Karr

unread,
Jan 22, 2015, 3:58:46 PM1/22/15
to spockfr...@googlegroups.com
I've seen some "greenfield" examples of setting up Spock tests in a Maven build, but what if I have an existing Maven build with Java unit tests, and I want to add or experiment with Spock tests, in addition to the Java unit tests?  I know that Surefire needs to be configured to now add "*Spec.java" (apparently not "*Spec.groovy", the logical choice) in addition to the existing "*Test.java".  What plugins and configuration do I need to add, and would I likely have existing plugins that now have to work with both Java and Groovy?

pnie...@gmail.com

unread,
Jan 22, 2015, 4:06:34 PM1/22/15
to spockfr...@googlegroups.com
Basically, all you need is a Groovy plugin (to compile your specs) and Groovy/Spock dependencies. The Maven build in https://github.com/spockframework/spock-example has the details (although there are other Groovy plugins for Maven by now).

Cheers,
Peter

On 22 Jan 2015, at 21:58, David Karr <davidmic...@gmail.com> wrote:

I've seen some "greenfield" examples of setting up Spock tests in a Maven build, but what if I have an existing Maven build with Java unit tests, and I want to add or experiment with Spock tests, in addition to the Java unit tests?  I know that Surefire needs to be configured to now add "*Spec.java" (apparently not "*Spec.groovy", the logical choice) in addition to the existing "*Test.java".  What plugins and configuration do I need to add, and would I likely have existing plugins that now have to work with both Java and Groovy?

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.
To post to this group, send email to spockfr...@googlegroups.com.
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.

Marcin Zajączkowski

unread,
Jan 22, 2015, 4:08:27 PM1/22/15
to spockfr...@googlegroups.com
I have never tried (with Gradle it works out of the box), but maybe
following sample project would be useful for you:
https://github.com/msakamoto-sf/maven3-junit-spock-testng-mixin
(JUnit, TestNG and Spock)

Btw, there is a comment in pom.xml:
> (mysteriously, ".groovy" dosn't work for Spock's xxxxSpec.groovy, but ".java" work!!)

Marcin

--
http://blog.solidsoft.info/ - Working code is not enough

John Allen

unread,
Mar 13, 2015, 12:19:10 PM3/13/15
to spockfr...@googlegroups.com
I am just learning Spock, and I would lovelovelove to use it.  But we need to run tests automatically in Jenkins via Maven on projects that also contain JUnit tests.

In other words we have an existing Eclipse (STS) Java project with JUnit tests, and I want to add Spock tests to it going forward.

Is this the way to do it?
  1. Follow instructions at http://mrhaki.blogspot.com/2011/01/spocklight-add-spock-support-to-java.html (updating version numbers).
  2. Convert Java project to Groovy project.  Can Jenkins run Groovy project JUnit and Spock tests?
  3. Create new Spock packages.  Or can I put them into folders that already have JUnit tests?
  4. Write Spock tests.
I hope that can work.  I have been reading and experimenting and spinning my wheels a lot.

John

Michael Kutz

unread,
Mar 13, 2015, 12:27:15 PM3/13/15
to spockfr...@googlegroups.com

Hi,

I've done Junit/Spock parallel projects several times. You can find my standard pom at https://github.com/mkutz/demonstration/blob/master/spock-testing/pom.xml.

Running mvn test will execute both, Junit and Spock and Jenkins should be able to find both test result files.

Kind regards
Micha

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spockframework+unsubscribe@googlegroups.com.
To post to this group, send email to spockframework@googlegroups.com.

Sten Aksel Heien

unread,
Mar 16, 2015, 4:30:50 AM3/16/15
to spockfr...@googlegroups.com
To put groovy and java code in same folder (what I like) you need something like this:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>minimalspock</groupId>
<artifactId>minimalspock</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spock Framework - Example Project</name>

<prerequisites>
<maven>3.0.5</maven>
</prerequisites>

<properties>
<java.version>1.7</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<!-- Without joint compilation - no dependencies between Java and Groovy (inheritance)-->
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>
<directory>${project.basedir}/src/main/java</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</source>
</sources>
<testSources>
<testSource>
<directory>${project.basedir}/src/test/java</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</testSource>
</testSources>

</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/*Spec.java</include>
<!-- Yes, .java extension -->
<include>**/*Test.java</include>
<!-- Just in case having "normal" JUnit tests -->
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

John Allen

unread,
Mar 16, 2015, 5:14:54 PM3/16/15
to spockfr...@googlegroups.com
Now that I have it figured out after much trial and error, making Spock work in an Eclipse Java project is super easy.

1. Start with STS (Eclipse Luna 4.4).
2. Install Groovy/Grails Plugin.
(I unchecked Quicksearch, the two Pivotal Servers, and Spring Dashboard because STS already has them.)
(I unchecked Grails because we don't need it.)
3. Install Spock Plugin (Jspresso).  (This may not be needed.)
4. Modify pom.xml.  I did not make any real changes to your standard pom, Michael Kutz (thank you very much) at https://github.com/mkutz/demonstration/blob/master/spock-testing/pom.xml.  Peter has those wonderful comments in the spock-example pom.xml, thank you, Peter.
5. Download new dependencies:  Select project in Package View and Run > Run As > Maven install.
6. Change Java project to Groovy project (since Groovy knows all about Java):  Select project, right click > Configure > Convert to Groovy Project.
7. Create a Spock test (even alongside a JUnit Java test) and Run > Run As > JUnit test.


Christopher Smith

unread,
Mar 20, 2015, 2:18:56 PM3/20/15
to spockfr...@googlegroups.com
Team,.

Does anyone have a sample project that include spring java components with this pom.xml and spock?



--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.
To post to this group, send email to spockfr...@googlegroups.com.

Przemysław Wojnowski

unread,
Mar 21, 2015, 3:40:13 AM3/21/15
to spockfr...@googlegroups.com
Hi.

Just take Spring config from other repositories, for example from here:
http://spock-framework.3207229.n2.nabble.com/Some-tutorials-for-Spock-Spring-Hibernate-Unitils-td7574043.html

Cheers,
Przemek

Jose Juan Reyes

unread,
Mar 22, 2015, 7:01:37 AM3/22/15
to spockfr...@googlegroups.com
What if you publish your code? Maybe I could help you...

Dmitry Senkovich

unread,
Feb 20, 2017, 11:03:03 AM2/20/17
to Spock Framework - User
Hi Michael,

I'm working with Maven and Spock, my E2E tests need to be parallized as it run too long. Could please provide configuration that enabled you to run Spock tests concurrently?

Thank you.


On Friday, March 13, 2015 at 7:27:15 PM UTC+3, Michael Kutz wrote:

Hi,

I've done Junit/Spock parallel projects several times. You can find my standard pom at https://github.com/mkutz/demonstration/blob/master/spock-testing/pom.xml.

Running mvn test will execute both, Junit and Spock and Jenkins should be able to find both test result files.

Kind regards
Micha

John Allen <jmane...@gmail.com> schrieb am Fr., 13. März 2015 17:19:

I am just learning Spock, and I would lovelovelove to use it.  But we need to run tests automatically in Jenkins via Maven on projects that also contain JUnit tests.

In other words we have an existing Eclipse (STS) Java project with JUnit tests, and I want to add Spock tests to it going forward.

Is this the way to do it?
  1. Follow instructions at http://mrhaki.blogspot.com/2011/01/spocklight-add-spock-support-to-java.html (updating version numbers).
  2. Convert Java project to Groovy project.  Can Jenkins run Groovy project JUnit and Spock tests?
  3. Create new Spock packages.  Or can I put them into folders that already have JUnit tests?
  4. Write Spock tests.
I hope that can work.  I have been reading and experimenting and spinning my wheels a lot.

John

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.
To post to this group, send email to spockfr...@googlegroups.com.

Michael Kutz

unread,
Feb 26, 2017, 11:04:24 AM2/26/17
to Spock Framework - User
Hey Dmitry,

to achieve parallel test execution, you should have a look into the Surefire or Failsafe plugin configuration options for forking and parallel execution.


You'll notice they are pretty much the same.

Kind regards,
Micha

Alexander Kriegisch

unread,
Feb 26, 2017, 12:36:00 PM2/26/17
to spockfr...@googlegroups.com
> to achieve parallel test execution, you should have a look into the
> Surefire or Failsafe plugin configuration options for forking and parallel
> execution.

And they should be because Failsafe is almost 100% identical to surefire, AFAIK always based on its sourcecode and always released in the same version, with only a few slight differences.
--
Alexander Kriegisch
https://scrum-master.de

Alexander Kriegisch

unread,
Feb 26, 2017, 12:36:18 PM2/26/17
to spockfr...@googlegroups.com
>> to achieve parallel test execution, you should have a look into the
>> Surefire or Failsafe plugin configuration options for forking and parallel
>> execution.

And here I accidentally deleted the very part of the quote necessary to understand my remark:

>> You'll notice they are pretty much the same.

Reply all
Reply to author
Forward
0 new messages