How can i introduce a method call before every method in geb-spock specification file

3.8k Views Asked by At

Problem Statement: How to induct code automatically just before start and end of test method in geb-spock specification file.

Geb-spock does not have something related to BeforeMethod or AfterMethod which gets executed just before start/end of test method. This feature is provided by testNG tool. As, Geb-spock uses junit internally and junit does not have this feature aswell, Geb-spock does not have this facility aswell.

But, I need to work out on solution using geb-spock where I need to induct a code just before start of test method and after completion of test method.

How can I solve my problem.

Thanks, Debasish

1

There are 1 best solutions below

0
On

please follow the below code for understanding:

import spock.lang.Specification
class SampleSpockSpec extends Specification {

   def setupSpec(){
          println "inside setupSpec method"
   }
   def setup(){
          println "inside setUP method"
   }
   def "playActivity1"(){
          println "Iam activity 1"
          given:
          println "Executing Given Stmt"

          when:
          println "Executing when Stmt"
          then:
          println "Executing Then Stmt"


   }

   def "playActivity2"(){
          println "Iam activity 2"
          given:
          println "Executing Given Stmt"

          when:
          println "Executing when Stmt"
          then:
          println "Executing Then Stmt"
   }
   def "playActivity3"(){
          println "Iam activity 3"
          given:
          println "Executing Given Stmt"

          when:
          println "Executing when Stmt"
          then:
          println "Executing Then Stmt"
   }
   def cleanup(){
          println "inside Clean up method"
   }
   def cleanupSpec(){
          println "inside cleanupSpec method"
   }

}

**Result:**

inside setupSpec method
inside setUP method
Iam activity 1
Executing Given Stmt
Executing when Stmt
Executing Then Stmt
inside Clean up method
inside setUP method
Iam activity 2
Executing Given Stmt
Executing when Stmt
Executing Then Stmt
inside Clean up method
inside setUP method
Iam activity 3
Executing Given Stmt
Executing when Stmt
Executing Then Stmt
inside Clean up method
inside cleanupSpec method