Monday, August 20, 2012

get feature and scenario name with cucumber-jvm

cucumber-jvm does not provide an easy way to get the feature name, scenario name or the step that is being executed. I had to find a workaround for this. For reference it is listed here. I am using the mirage project that I created a while back that provides a few utility classes around reflection. The ReflectionUtils.getFieldInObject uses that.

I created an aspect and weaved the info.cukes:cucumber-junit jar file with the aspect.
 public aspect ReportFeatureScenarioStep {  
      pointcut running_feature() : execution(public * cucumber.junit.FeatureRunner.run(..));  
      pointcut running_scenario() : execution(public * cucumber.junit.ExecutionUnitRunner.run(..));  
      pointcut running_step() : execution(public * cucumber.junit.JUnitReporter.match(..));  
 // Not used. These are here for reference only  
 //     pointcut completing_feature() : execution(public * cucumber.junit.FeatureRunner.run(..));  
 //     pointcut completing_scenario() : execution(public * cucumber.junit.ExecutionUnitRunner.run(..));  
 //     pointcut completing_step() : execution(public * cucumber.junit.JUnitReporter.result(..));  
      before() : running_feature() {  
           ParentRunner pr = (ParentRunner) thisJoinPoint.getTarget();  
           System.out.println(pr.getDescription().toString());  
      }  
      before() : running_scenario() {  
           ParentRunner pr = (ParentRunner) thisJoinPoint.getTarget();  
           System.out.println(pr.getDescription().toString());  
      }  
      before() : running_step() {  
           StepDefinitionMatch m = (StepDefinitionMatch) thisJoinPoint.getArgs()[0];  
           Step step = (Step) ReflectionUtils.getFieldInObject(m, "step");  
           System.out.println(step.getKeyword() + " " + m.getStepName());  
      }  
 // Not used. These are here for reference only       
 //     after() : completing_feature() {  
 //          ParentRunner pr = (ParentRunner) thisJoinPoint.getTarget();  
 //          System.out.println("AFTER FEATURE : -->" + pr.getDescription() + "<--");  
 //     }  
 //       
 //     after() : completing_scenario() {  
 //          ParentRunner pr = (ParentRunner) thisJoinPoint.getTarget();  
 //          System.out.println("AFTER SCENARIO : -->" + pr.getDescription() + "<--");  
 //     }  
 //       
 //     after() : completing_step() {  
 //          System.out.println("AFTER STEP ");  
 //     }  
 }  

This aspect is weaved into the cucumber-junit by doing this:

               <plugin>  
                   <groupId>org.codehaus.mojo</groupId>  
                     <artifactId>aspectj-maven-plugin</artifactId>  
                     <version>1.4</version>  
                     <executions>  
                          <execution>  
                               <goals>  
                                    <goal>compile</goal>  
                                    <goal>test-compile</goal>  
                               </goals>  
                          </execution>  
                     </executions>  
                     <configuration>  
                          <source>1.6</source>  
                          <complianceLevel>1.6</complianceLevel>  
                          <verbose>false</verbose>  
                          <showWeaveInfo>false</showWeaveInfo>  
                          <target>1.6</target>  
                          <weaveDependencies>  
                               <weaveDependency>  
                                    <groupId>info.cukes</groupId>  
                                    <artifactId>cucumber-junit</artifactId>  
                               </weaveDependency>  
                               <weaveDependency>  
                                    <groupId>info.cukes</groupId>  
                                    <artifactId>cucumber-java</artifactId>  
                               </weaveDependency>  
                          </weaveDependencies>  
                     </configuration>  
                </plugin>  

Thursday, August 16, 2012

what is your favorite design pattern ?

In the last few technical interviews I have introduced this new question on design patterns -- "What is your favorite design pattern?"

The responses I got from candidates was - MVC, Singleton, DAO, Facade, Factory etc.

But what I don't get here is how can you have a favorite design pattern?

There are so many design patterns. The underlying idea is to use the right one for the right purpose and to solve the right problem. It does not matter if you like one or the other. Your level of understanding of one pattern may be high and other may be low - and that is perfectly fine. However how can I have a favorite design patten?

Whats happening

Almost a year since I blogged. Whats happening! For 8 hours a day it has been fun working with a new team on a new project. The work after work has been equally exciting. I am working on a new project - gherkinsalad - meant for automation using java, cucumberjvm, webdriver. The website is http://www.gherkinsalad.com.