Groovy and Grails Groovy and Grails Testing & Quality Assurance 1 — Questions and Answers
Question 1: Which testing framework is the default and most commonly used for writing tests in Grails applications?
- JUnit 4
- TestNG
- Spock (Correct answer)
- Mockito
Correct answer: Spock
Spock is the default testing framework in Grails, providing expressive, BDD-style specifications written in Groovy.
Question 2: In a Spock specification, what block is used to define the expected outcome or assertions?
- given
- when
- then (Correct answer)
- and
Correct answer: then
The 'then' block in Spock is used to define assertions and verify the expected behavior after the action in the 'when' block.
Question 3: Which Grails test phase is specifically designed for testing interactions between multiple components or layers?
- Unit tests
- Integration tests (Correct answer)
- Functional tests
- Smoke tests
Correct answer: Integration tests
Integration tests in Grails load the full application context, allowing you to test interactions between controllers, services, and the database.
Question 4: Which annotation is used in Grails to mock a collaborating bean in a unit test?
- @Mock (Correct answer)
- @MockBean
- @Stub
- @GrailsUnitTest
Correct answer: @Mock
The @Mock annotation in Spock is used within a Grails unit test to create a mock implementation of a collaborating class.
Question 5: What is the purpose of the `where` block in a Spock data-driven test?
- To define test setup
- To supply parameterized input data (Correct answer)
- To clean up after tests
- To define mocks
Correct answer: To supply parameterized input data
The 'where' block in Spock provides a data table or variables that parameterize a test, running it multiple times with different inputs.
Question 6: Which command runs only the unit tests in a Grails application?
- grails test-app unit: (Correct answer)
- grails run-tests
- grails test unit
- grails check
Correct answer: grails test-app unit:
The command `grails test-app unit:` filters and runs only unit test phase specifications in a Grails project.
Which testing framework is the default and most commonly used for writing tests in Grails applications?