Thursday, 5 March 2015

A Brief rendezvous with Spock (Part II)

FEATURE METHOD

The feature method is somewhat similar to @Test annotated methods in JUnit. Here, all the test related jobs are being done but in a different way as its body consists of the following logical blocks: a) setup b) when c) then d) expect e) cleanup and f) where Now, let’s describe each of these blocks with reference to the below mentioned code block, so that it becomes easy to understand

def "Second Test2"()
{
  
   
setup: "Initialize variables here"
  println "***** Variables Initialized *******"
   
 when: "Call Operations to be executed"
  println "Operations invokded"
   
 then: "Compare Actual Result with Expected one"
  println "${param1}<--->${param2}"
    
 cleanup: "CleanUp Things"
  println "*** CleanUp Done **** "
    
 where:
  param1 | param2
  'ok1' | "asd|asd|||tgh||ASD"
  'ok9909'| "asd|asd|||990||ASD"
    
  
   
  
}
Now from the above mentioned code block, it becomes quite evident that the name of the Feature Method is a user-defined one which has been provided in quotes after def i.e. “Second Test2”, So we should always try to provide a meaningful name to the Feature Methods. Each feature method must have atleast one explicit block and the blocks should not be nested.

a) setup

In this block all the initialization and declarations, which needs to be done before each tests are placed. It is similar to given: block.

b) when & c) then

Basically the when: and then: blocks appear in pairs and there may be multiple appearances of them in the feature methods. In the when: block we excute the code block which needs to be tested and in the then: block, we compare the output the when: block method call (i.e. actual Result) with the expected Result. Please note that the conditions are written in plain Boolean expressions so there is no need of using any Assertion statements. There are multiple tools to handle exceptions in the then: block like:
thrown(MyCustomException)

or

def e = thrown(MyCustomException)

or

MyCustomException e = thrown()
Sometimes we don't want exceptions to be thrown out, then we could go for:
notThrown(MyCustomException)

d) expect

expect: block is used when we want to club both when: and then: in a single expression.

e) cleanup

From the name it is quite evident, resource cleanup activities may be done here.

f) where:

It is used to create a Data-Driven feature methods, like we have used in the above code snippet to create a parameterized test cases, and it is usually comes last in the feature method. The block defined in the feature methods presents us with a BDD i.e. Behaviour Driven Design. The last component in the specification is:

HELPER METHODS

Sometimes the common code blocks in feature methods are separated out from and Helper Methods are created from them to avoid redundancy. So this is in short my introduction with Spock. Hope this post would bring some purpose to readers. Any suggestions and feedback are always welcome. Please share your view and Keep coding……..
View Subhankar Paul's profile on LinkedIn

No comments:

Post a Comment