Search code examples
javacucumber-serenity

@After doesn't work (Cucumber with SenerityBDD)


My issue: I am using Cucumber with senerityBDD JAVA. In my project, @after not working after scenario as senerity document i read.

My hook class:

package com.TestSuite;

import cucumber.api.CucumberOptions;
import cucumber.api.java.After;
import net.serenitybdd.core.Serenity;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
    
import static com.netdreamers.utils.Performance.PerformanceController.*;

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(features = "src/test/resources/features", tags = "@testCase")

public class TestSuite{

    @BeforeClass
    public static void beforeClass(){
        System.out.println("Start BeforeClass");   
   }

    @AfterClass
    public static void afterClass() {
       System.out.println("Start AfterClas");   

    }

    @After
    public void afterScenario(){
        System.out.println("After Scenario");
    }
}

My Feature file:

Feature: Access to Url

  Background:

  Scenario Outline: Access to Url
    Given I access to the Url at line "<index>"
    Examples:
      | index |
      | 0     |
      | 1     |

As expectation, @after annotation should work, it should be called after scenario. So Please someone could tell me the reason the @after annotation not work after 2 loop when the Scenario Ouline repeats for Examples table "index" variable?


Solution

  • AFAIK @After only works in step code. Update your options to

    @CucumberOptions(features = "src/test/resources/features", glue = 
            "com.testsuite.steps", tags = "@testCase" )
    

    And move the after code to a new step class

     package com.testsuite.steps;
    
     import cucumber.api.java.After;
    
     public class TestSteps {
        
            @After
            public void afterScenario(){
                System.out.println("After Scenario");
            }
     }