Groovy and Grails Groovy and Grails Testing & Quality Assurance 2 — Questions and Answers
Question 1: In Spock, what does the `1 * service.method()` expression inside a `then` block verify?
- The method returned 1
- The method was called exactly once (Correct answer)
- The method took 1ms
- The method has 1 argument
Correct answer: The method was called exactly once
Interaction-based assertions in Spock use the format `N * mock.method()` to verify that a mocked method was called exactly N times.
Question 2: Which Grails trait is mixed into a controller unit test specification to provide controller-specific testing support?
- ServiceUnitTest
- ControllerUnitTest (Correct answer)
- DomainUnitTest
- HibernateSpec
Correct answer: ControllerUnitTest
The `ControllerUnitTest` trait provides mock request, response, and other web-tier helpers needed when unit testing Grails controllers.
Question 3: What annotation marks a Grails domain class test specification to enable GORM persistence in integration tests?
- @Integration (Correct answer)
- @Transactional
- @DomainUnitTest
- @Rollback
Correct answer: @Integration
The `@Integration` annotation on a Spock specification tells Grails to load the full application context so that GORM and the database are available.
Question 4: Which Spock block is used for setup code that runs before each feature method?
- setup (Correct answer)
- setupSpec
- given
- before
Correct answer: setup
The `setup` block in Spock runs before each individual feature method, similar to JUnit's @Before annotation.
Question 5: In Grails functional testing, which library is commonly used to simulate browser interactions?
- Geb (Correct answer)
- Selenium Grid
- Cucumber
- Arquillian
Correct answer: Geb
Geb is a Groovy browser automation library built on top of WebDriver, and it integrates naturally with Grails for functional tests.
Question 6: What does the `cleanup` block do in a Spock specification?
- Deletes test class files
- Runs after each feature method to release resources (Correct answer)
- Clears the test database schema
- Resets Spring beans
Correct answer: Runs after each feature method to release resources
The `cleanup` block in Spock executes after each feature method, even if the test fails, making it ideal for releasing resources.
In Spock, what does the `1 * service.method()` expression inside a `then` block verify?