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
14
votes
4 answers

Grails / Spock: How to mock single method within class where method is called from within the class itself?

Given the following, how do I mock processMessage() using Spock, so that I can check that processBulkMessage() calls processMessage() n times, where n is the number of messages within a BulkMessage? class BulkMessage { List messages } class…
John
  • 10,837
  • 17
  • 78
  • 141
14
votes
1 answer

What is the definition of double right arrow operator ( >> ) in Spock?

There are code snippets in this Spock wiki page that involve double right arrow operators (>>). For example: setup: subscriber.isAlive() >> true So, what does the >> operator mean in this case? Thank you very much.
JBT
  • 8,498
  • 18
  • 65
  • 104
14
votes
4 answers

How to create Spock mocks outside of a specification class?

We combine Spock tests with Spring's @ContextConfiguration so we can build beans in a spring context and then use Spock for the actual testing. We would like to inject spock mocks into our spring beans. For Mockito there is an extension which allows…
Jan Thomä
  • 13,296
  • 6
  • 55
  • 83
14
votes
2 answers

running grails 2.1.3 tests in Intellij Idea: Bizarre error in Spock test: Cannot add Domain class [class x.y.Z]. It is not a Domain

I am in the process of upgrading to grails 2.1.x, and need to redo some of my old-style tests. I just added a new test to my spock Spec, and for this test I need to mock an additional Domain class. Before this, I had: @Mock([Event, EventType]) Now…
Luis Muñiz
  • 4,649
  • 1
  • 27
  • 43
14
votes
3 answers

How to Run a Spock Test inside Eclipse

I try to run my first Spock Test inside Eclipse, and it does not work. I added all the Maven dependencies and plugins in my pom.xml, but when I run my test with jUnit, there is a popup windows with this Warning message : "No jUnit tests found". Did…
Emmanuel Demey
  • 2,158
  • 4
  • 18
  • 21
13
votes
1 answer

Unit test Groovy2.0 with Spock : setup( )

I am writing unit test using Spock for groovy-2.0 , and using gradle to run. If I write following the test pass. import spock.lang.Specification class MyTest extends Specification { def "test if myMethod returns true"() { expect: …
Masa
  • 311
  • 1
  • 2
  • 5
13
votes
1 answer

Mock multiple calls to the same method in spock

I am currently writing unit testcase for a groovy application class StorePage{ .. .. str1 = obj.getDateBasedOnValue("A"); str2 = obj.getDateBasedOnValue("B"); } Test class classStorePageSpec extends Specification{ Obj obj =…
Supersic
  • 232
  • 2
  • 13
13
votes
3 answers

Run a specific test in a single test class with Spock and Maven

I am using Spock framework for testing (1.0-groovy-2.4 release). Junit offers this option to run a specific test using the command line (with Maven): mvn -Dtest=TestCircle#mytest test Question: How can I do this with Spock? This version has a…
Rafik EL YAAGOUBI
  • 392
  • 1
  • 5
  • 20
13
votes
1 answer

Spock label combinations

There | are | so many | Spock | spec examples of how to use its labels, such as: // when -> then label combo def "test something"() { when: // blah then: // blah blah } Such label combinations as: when -> then given -> when ->…
smeeb
  • 27,777
  • 57
  • 250
  • 447
13
votes
4 answers

@Mock/@InjectMocks for groovy - spock

In JUnit / Mockito we have 2 extremly useful annotations: @Mock and @InjectMocks. In my new project i started using groovy with spock for testing, I'm wondering if there is a replacement for mentioned annotations?
zibi
  • 3,183
  • 3
  • 27
  • 47
13
votes
3 answers

Can I override RESTClient default "HttpResponseException" response to >399 Return Codes?

I'm using the Groovy RESTClient class to do write some (spock) Acceptance tests for Java WebServices I've been authoring. One frustration I've had is in testing the responses... 200 Status's are easy: when: def result =…
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
12
votes
2 answers

Programmatically skip a test in Spock

How do I programmatically skip a test in the Spock framework? I know I can annotate a test with @Ignore to skip it, or use @IgnoreIf to skip tests based on environmental variables and the like. But is there a way to run arbitrary code that decides…
fzzfzzfzz
  • 1,248
  • 1
  • 12
  • 22
12
votes
1 answer

mock out return of a method base on the number of invocation only in spock

Is it possible to mock out the return value of a method in spock based on the nth time it was called? Note that I don't want to specify the parameters passed in because it does not matter for a specific test case. For example, for the first call it…
froi
  • 7,268
  • 5
  • 40
  • 78
12
votes
1 answer

How to use instance variable in where section of Spock Test

Spock only allows static variables to be accessed from where block. Is there any workaround using which Instance variables can be used inside the where block ?
12
votes
1 answer

How do I specify a Range instead of List in the 'where:' block of a Spock specification

The following example code: class MySpec extends spock.lang.Specification { def "My test"(int b) { given: def a = 1 expect: b > a where: b << 2..4 } } throws the following compilation error:…
AndrewW
  • 678
  • 6
  • 18