Grails Spock Testing Where Block Passing Dynamic Objects



Grails Spock testing passing dynamic objects in where block
To fix "Only @Shared and static fields may be accessed from here" issue in spock integration test with where block we need to use @Shared annotation while declaring variables and need to initialize them in setupSpec() method
Sample implementation
@TestMixin(IntegrationTestMixin)
class SpockExampleIntegrationTests  extends Specification {
    @Shared String email1
    @Shared String email2
 
    void setup() {
          email1 = "mannejkumar@gmail.com"
          email2 = "jk.manne@yahoo.com"
    }
 
    def testGetEmployeeById() {
  
        setup:
        Employee employee
  
        when:
        employee = employeeService.getEmployeeById(employeeId)

        then:
        employee.email == result

        where:
        sno |   employeeId     |     result     
        1   |   1              |     email1
        2   |   2              |     email2
   }
}

Categories: ,