'I am getting io.cucumber.java.InvalidMethodSignatureException in Cucumber Java

My Feature file -
Feature: Open google page
 
  @Sanity
  Scenario: Open google search
    Given User open google search
    Then enter text "hi" in search bar
    And close the browser 

###########################################################################

Page Object class -
package pageObjects;

import org.openqa.selenium.Keys;    
import org.openqa.selenium.WebDriver;    
import org.openqa.selenium.WebElement;      
import org.openqa.selenium.support.CacheLookup;   
import org.openqa.selenium.support.FindBy;   
import org.openqa.selenium.support.PageFactory;

public class GoogleObjects {
    
    public WebDriver ldriver;
    
    public GoogleObjects(WebDriver rdriver) {
        
        ldriver = rdriver;
        PageFactory.initElements(rdriver, this);
    }

    @FindBy(name = "q")
    @CacheLookup
    WebElement search;
    
    
    public void Search(String searchTxt) {
        
        search.sendKeys(searchTxt);
        search.sendKeys(Keys.DOWN, Keys.ENTER);
        
    }
    
    public void btnClick() {
        
        ldriver.close();
        
    }
}

############################################################################

StepDifinition class - 
package stepDefinitions;

import org.openqa.selenium.WebDriver;    
import io.cucumber.java.en.*;    
import pageObjects.GoogleObjects;    

public class GoogleSearch {

    public WebDriver driver;

    
    @Given("^User open google search$")
    public void user_open_google_search() throws Throwable {
        
        driver.get("https://www.google.com");
       
    }


    @Then("^enter text \"([^\"]*)\" in search bar$")
    public void enter_text_something_in_search_bar(String TxT) throws Throwable {
        
        GoogleObjects gb = new GoogleObjects(driver);
        gb.Search(TxT);
    }
    
    @And("^close the browser$")
    public void close_the_browser() throws Throwable {
        
        GoogleObjects gb = new GoogleObjects(driver);
        gb.close();
    }
}
##########################################################################

Test Runner class -
package runner;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;    
import io.cucumber.junit.CucumberOptions;    

@RunWith(Cucumber.class)  
@CucumberOptions(  
                    features = {".//Features/google.feature"},   
                    glue = {"stepDefinitions"},    
                    monochrome=true,    
                    tags = "@Sanity",    
                    plugin = {"pretty","html:target/reports.html" ,  
  "json:target/reports.json"}   
    )

public class testRunner {

}
######################################################################

hooks class - 
package stepDefinitions;

import java.io.IOException;      
import org.openqa.selenium.WebDriver;     
import org.openqa.selenium.firefox.FirefoxDriver;     
import io.cucumber.java.After;    
import io.cucumber.java.Before;   

public class hooks {

        WebDriver driver;
    
        @Before
        public WebDriver setUp() throws IOException {
            
            System.setProperty("webdriver.chrome.driver","*:\\****\\***\\Drivers\\chromedriver.exe");
            
            driver = new ChromeDriver();
            
            return driver;
        }
        
        @After
        public void tearDown() {
            
            driver.quit();
        
        }
    }

########################################################### I created a Maven project with cucumber framework and wrote a feature file, step definitions, test runner and hooks file. When I run my test runner I am getting "io.cucumber.java.InvalidMethodSignatureException", when I import io.cucumber.java.before/after but my test is running when I import from junit, but @before and @after doesn't work in cucumber if we import from juint. I not understanding why I am getting InvalidMethodSignatureException. I tried providing order=0 for @Before annotation. It doesn't work Above is the code, please help me.



Solution 1:[1]

You get this issue because methods annotated with io.cucumber.java.Before have to be void.

In your example it returns WebDriver. This is why you get InvalidMethodSignatureException.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexey R.