Questions tagged [spock]

Spock is a testing and specification framework for Java and Groovy applications. The highly expressive specification language makes it easy to create and maintain the code.

Spock is a testing and specification framework for and applications. The highly expressive specification language makes it easy to create and maintain the code.

The Spock framework is compatible with most IDEs and build tools and the specifications can be run and checked from inside your IDE. As long as it can run , it can run Spock.

Getting Started:

Testing Tutorials:

Github Examples:

2357 questions
128
votes
4 answers

Difference between Mock / Stub / Spy in Spock test framework

I don't understand the difference between Mock, Stub, and Spy in Spock testing and the tutorials I have been looking at online don't explain them in detail.
letter Q
  • 14,735
  • 33
  • 79
  • 118
72
votes
7 answers

Spock - Testing Exceptions with Data Tables

How can exceptions be tested in a nice way (e.g. data tables) with Spock? Example: Having a method validateUser that can throw exceptions with different messages or no exception if the user is valid. The specification class itself: class User {…
René Scheibe
  • 1,970
  • 2
  • 14
  • 20
53
votes
3 answers

Spock throw exception test

I test Java code with Spock. I test this code: try { Set availableActions = getSthAction() List goodActions = getGoodAction() if (!CollectionUtils.containsAny(availableActions ,goodActions )){ throw new…
Piotr Sobolewski
  • 2,024
  • 4
  • 28
  • 42
46
votes
1 answer

Spock: can an interaction defined in setup() be replaced in a test case?

I'm struggling to understand something about Spock interactions in a Groovy unit test. I have the following types: public interface Bar { public String getMessage(); } public class Foo { private Bar bar; public void setBar(Bar bar) { …
John Q Citizen
  • 3,138
  • 4
  • 26
  • 31
42
votes
4 answers

How to start Spring Boot app in Spock Integration Test

What is the best way to run an integration test (e.g., @IntegrationTest) with Spock? I would like to bootstrap the whole Spring Boot application and execute some HTTP calls to test the whole functionality. Spring Boot app starts in this working…
kuceram
  • 3,795
  • 9
  • 34
  • 54
42
votes
6 answers

How to do argument capture with spock framework?

I have some Java stuff like this: public interface EventBus{ void fireEvent(GwtEvent event); } public class SaveCommentEvent extends GwtEvent{ private finalComment oldComment; private final Comment newComment; public…
Alex Luya
  • 9,412
  • 15
  • 59
  • 91
41
votes
1 answer

Verify no exceptions were thrown in Spock

I am brand new to Spock and perused their online docs. I have a test case where I need to verify that my fixture's interaction with a non-mock collaborator does not produce an exception: class FizzSpec extends Specification { def "no exception…
smeeb
  • 27,777
  • 57
  • 250
  • 447
37
votes
1 answer

what does >> and 1* means in this groovy statement?

I am working on grails/groovy project and while reading their test documentation came across this line of code 1 * myService.prova() >> { true } I am unable to understand what does 1 * means here also as >> is right shift operator what does it mean…
Avinash Agrawal
  • 1,038
  • 1
  • 11
  • 17
33
votes
1 answer

Is there any way to do mock argument capturing in Spock

I have looked around and tried different things to no avail. The examples out there on the interwebs are few, and IMHO pretty simple. My use case: (the 'itocNetworkHandler' below is the mock) when: "we're doing stuff" StandardResponse response =…
Mathias
  • 3,879
  • 5
  • 36
  • 48
32
votes
4 answers

When Spock's @Shared annotation should be preferred over a static field?

There is not much to add, the whole question is in the title. Consider these two instances of class Foo used in a Spock specification. @Shared Foo foo1 = new Foo() static Foo foo2 = new Foo() Overall, I know the idea behind @Shared annotation but…
topr
  • 4,482
  • 3
  • 28
  • 35
32
votes
1 answer

Spock- ignore spec method for a subclass

I have a test spec in Spock and I want to apply the same tests to another alternative with exception of one method. I extend the original spec, implement some custom stuff related to the alternative and would really like to exclude the execution of…
ps-aux
  • 11,627
  • 25
  • 81
  • 128
32
votes
4 answers

Running spock unit tests with Maven

On a previous project I used the Spock testing framework to unit test my Java code. I found this really productive so I am trying to add Spock tests to my current project which uses Maven as its build tool (The previous project used Gradle). While…
Neil Stevens
  • 6,745
  • 4
  • 14
  • 7
31
votes
6 answers

Behavior Driven Development for java - what framework to use?

For the ongoing projects and for improving our development process we considered adopting TDD as development philosophy. While researching for best practices and how to "sell" the new approach to my colleagues/ developers I came across BDD and found…
Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
25
votes
2 answers

Simple Mockito verify works in JUnit but not Spock

Using the most basic example from Mockito's examples page, I am able to run successfully in JUnit. However, when I run the same test in Spock, it fails. JUnit/Java version (this passes): import org.junit.Test; import java.util.List; import…
Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
24
votes
1 answer

Determine order of execution of Spock tests

Is there a way to set the order in which tests are executed within an Spock Specification? For example: class MySpec extends IntegrationSpec { def 'test A'... def 'test B'... } I want 'test A' to be execute always before 'test B' This is…
Tomas Romero
  • 8,418
  • 11
  • 50
  • 72
1
2 3
99 100