Clear Your Spock Interview Successfully with this list of Spock Interview Questions:
In this Spock Tutorials for All, we explored all about Integration and Functional Testing in Spock in our previous tutorial.
This tutorial will cover the most commonly asked interview questions around the Spock framework.
We will also try to understand Spock fixture methods and built-in extensions support which make Spock a really powerful tool for a lot of testing types like Unit, Integration, and end-to-end.
Expert Quiz on Spock Interview Questions
Get hired in top MNCs of your choice with this expert quiz on Spock Interview Questions. This quiz is perfectly ideal for freshers, intermediate, and experienced professionals preparing for Spock-based interview.

Most Popular Spock Interview Questions
Enlisted below are some of the most commonly asked Spock Interview Questions with answers and examples.
Let’s Explore!!
Q #1) Can a Spock test have multiple when and then blocks?
Answer: It’s recommended to have small tests or scenarios, as trying to do a lot of things in a single test might be a code smell. Having said that, it’s perfectly valid to have multiple when and then blocks in a test. The test will be considered to be successful only when all the then blocks are passing.
Let’s see an example to illustrate this:
def "illustrate multiple when and then blocks"() {
given:
def input1 = 50
def input2 = 100
def result
when:
result = input1 + input2
then:
result == 150
when:
result = input2 - input1
then:
result == 50
}
In the above code block, you can see that we have 2 when and then blocks.
Please note the below points:
- The blocks are executed in the order of their appearance, i.e. sequentially.
- Failure of anyone then block will lead to the test being failed.
- Assertions in all the then blocks should pass for the overall test to succeed.
Q #2) What fixture methods are available in Spock?
Answer: Fixture methods are like callbacks, which are called when a particular event hook is triggered.
Spock provided 4 test fixtures which are triggered against different events:
- setupSpec – Runs once before the entire Spec file execution starts.
- cleanupSpec – Runs once when all the tests in the given Spec file are executed
- setup – Runs once before each test in the specification.
- cleanup – Runs once after each test in the specification.
Let’s see a code sample to illustrate the fixture methods:
class TestFixtureSpec extends Specification {
def setupSpec() { println "in setup spec!"
}
def cleanupSpec() {
println "in cleanup spec!"
}
def setup() {
println "in setup!"
}
def cleanup() {
println "in cleanup!"
}
def "test spec1"() {
given:
println "****test spec1****"
}
def "test spec2"() {
given:
println "****test spec2****"
}}
Below is the output of the above code sample:
in setup spec!
in setup!
****test spec1****
in cleanup!
in setup!
****test spec2****
in cleanup!
in cleanup spec!
As shown above, you can notice that the setup and cleanup spec is called just once for the entire spec and the setup & cleanup steps/fixtures are called once per test.
Q #3) Can Spock tests be used for testing REST-based services?
Answer: Yes, Spock framework can be used for creating E2E or integration tests for deployed rest services using common Java libraries like Rest template, etc. (Also, please note that, Spock can also be used for running tests for Spring-boot-based applications and with other frameworks like Selenium).
Let’s see this with a simple example that uses Spring’s RestTemplate class to perform a get operation on a public-hosted API and checks the response to be non-null.
Example:
class RestApiIntegrationSpec extends Specification {
def "check rest api status"() {
when: "a rest call is performed to the status page"
RestTemplate restTemplate = new RestTemplate()
String response = restTemplate.getForObject("https://httpbin.org/get", String.class)
then:
response != null
}
}
In the above example, you can refer to the Spock specification that’s being used to assert the response of a public API.
Q #4) What are the limitations of the Spock framework?
Answer: Although the learning curve for the Spock framework is not as steep as it’s easy to learn, its declarative syntax makes it highly readable.
Meanwhile, there are a few points that could be considered:
- For applications on the Java codebase, using Spock will result in adding a new language stack i.e. Groovy.
- Spock tests run slightly slower than native JUnit tests.
- IDE support for Spock is not as good as for other frameworks like JUnit.
Despite all the above points, still, the advantages of the Spock framework outweigh the small list of cons that Spock has.
Q #5) Explain some of the built-in extensions of the Spock framework.
Answer: Spock provides a lot of built-in extensions/hooks/ triggers which are mostly annotation-based (We did see a couple of them in the test fixtures section/question).
Let’s see some of the in-built discussion with examples:
@Ignore: To prevent a feature (or individual method) from getting executed. Using a simply decorated method (individual test method) or an entire spec will ensure that the annotated method or class does not get executed.
@Ignore
def "check case-insensitive equality of 2 strings"() {
given: "two input strings"
String str1 = "hello"
String str2 = "HELLO world"
when: "strings are lowercased"
str1 = str1.toLowerCase()
str2 = str2.toLowerCase()
then: "equal strings should return success"
str1 == str2
}
@IgnoreRest: This annotation is useful when you just want to select one and execute the rest of the methods of the given specification.
@IgnoreRest
def "check case-insensitive equality of 2 strings"() {
given: "two input strings"
String str1 = "hello"
String str2 = "HELLO world"
when: "strings are lowercased"
str1 = str1.toLowerCase()
str2 = str2.toLowerCase()
then: "equal strings should return success"
str1 == str2
}
def "check addition of 2 numbers"() {
given:
int input1 = 10
int input2 = 25
expect:
input1.getClass().toString() == "class java.lang.Integer"
input2.getClass().toString() == "class java.lang.Integer"
input1 <= Integer.MAX_VALUE
input2 >= Integer.MIN_VALUE
when:
int result = input1 + input2
then:
result == 35
}
As seen in the above example, the method annotated with @IgnoreRest will be executed and the rest of the tests will be ignored.
@IgnoreIf: This annotation is a conditional ignore.
For Example: If You don’t want to run some tests on Mac OS, then you can use a combination of @IgnoreIf with System.getProperty(“os.name”) which will ensure that the tests will be run only if the matching OS is found.
Let’s try to understand this with the below code sample:
@IgnoreIf({ System.getProperty("os.name").contains("Mac") })
def "check case-insensitive equality of 2 strings"() {
given: "two input strings"
String str1 = "hello world"
String str2 = "HELLO world"
when: "strings are lowercased"
str1 = str1.toLowerCase()
str2 = str2.toLowerCase()
then: "equal strings should return success"
str1 == str2
}
In the above code sample, we’ve used @IgnoreIf annotation with a condition on System.getProperty which will check for “Mac” in the property value and ignore only if the condition match is successful.
Let’s see one more extension here i.e. @Timeout: This helps to mention a timeout value in the unit of your choice for the test under execution and if the timeout threshold is breached, the test will throw an exception.
Another important point to note here is that the @Timeout annotation can be mentioned over the complete spec as well and this will combine the duration of all the individual tests and throw an exception in case of a threshold breach.
@Timeout(value=10, unit= TimeUnit.MILLISECONDS)
class SampleSpec extends Specification {
def "check case-insensitive equality of 2 strings"() {
//test1
}
def "check addition of 2 numbers"() {
//test2
}
}
In the above code, if the total execution time of spec exceeds 10 ms then the scenario execution will fail. You can see the output with error details in the error console.

Similar to the above-mentioned extensions, there are a couple of other built-in extensions as well like:
@Requires: This requires a specific condition to be true.
@Issue: To link any defects associated with the test case, etc.
These extensions add a lot of flexibility and power to Spock specifications and give a lot of control for the test execution.
Conclusion
Thus, we have covered the most popular Spock Interview Questions here in this tutorial. The learning curve for Spock is low because the language Groovy follows a declarative programming style and is highly readable.
Although relatively new, Spock is gaining popularity as a framework of choice for writing different kinds of tests in Java or Groovy-based applications.
Hope, you enjoyed all the informative tutorials in this Spock Series. We are indeed sure that these tutorials would have enriched your knowledge and understanding of Spock.
PREV Tutorial | FIRST Tutorial






