Search code examples
springjunitcucumber-jvm

Cucumber-spring is not finding Step Definitions


I need help!

so the following code works for me (Pure JUnit code)

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:/importMasterConfig.xml")
public class FeatureWrittenInJavaUsingSteps {
@Before()
public void setup(){
    /*do something*/
}

@After
public void tearDown()
{
    /*Do something*/
}

@Autowired
ItemServiceController service;

@Test
public void callingStepFunctionsExample(){

    ItemServiceControllerTestsSteps steps = new ItemServiceControllerTestsSteps(service);
    steps.I_prepare_a_X_item_for_the_X_dealer("only images and pricing", "furniture");
    steps.I_perform_the_X_inventory_service_call("createItem");
    steps.I_should_get_the_X_response_code("200");
    steps.the_inventory_service_response_result_should_be_a_X_object("Vertical Item");
}
}

However, when I try to run this code using the Cucumber Feature, it can't seem to build correctly. I am assuming I am setting up the project wrong.

Here is my Step code:

@ContextConfiguration("classpath:cucumber.xml")
public class ItemServiceControllerTestsSteps {

//Common variables across steps - currently only local.
private VerticalItem itemToCreate;
private ServiceResponse response;

//Step specific variables.
@Autowired
private ItemServiceController itemService;

public ItemServiceControllerTestsSteps(ItemServiceController service){
   itemService = service;
}    

@Before()
public void setup(){/*Do something*/}

@After()
public void tearDown(){/*Do Something*/}

@Given("^I prepare a \"(.*)\" item for the \"(.*)\" dealer$")
public VerticalItem I_prepare_a_X_item_for_the_X_dealer(String itemType, String dealerType){ //Step function and factory in one.
/*Do stuff*/}

@When("^I perform the \"(.*)\" inventory service call$")
public void I_perform_the_X_inventory_service_call(String actionType){
 /*Do Stuff*/}

@Then("^I should get the \"(.*)\" response code$")
public void I_should_get_the_X_response_code(String codeType){/*Do stuff*/}

@Then("^the inventory service response result should be a \"(.*)\" object$")
public void the_inventory_service_response_result_should_be_a_X_object(String expectedClassType){ /*Do Stuff*/}


}

Here is my cucumber.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="cucumber.runtime.java.spring stepDefinitions"/>
    <context:annotation-config/>

    <import resource="classpath:importMasterConfig.xml"/>
</beans>

Finally here is my Runner Class:

 @RunWith(Cucumber.class)
 @CucumberOptions(plugin = {"pretty", "rerun:rerun.txt", "html:target/local-html-report/"},
 glue = "stepDefinitions.ItemServiceControllerTestsSteps")
 public class CucumberRunner {}

If someone can please enlighten me why the JUnit runner works and the cucumber one doesn't I would be a very happy camper!


Solution

  • In the above code, I did a few things wrong but lets cover the big ones.

    1) My Glue code String was incorrect, I need to pass in the package name, not the file name (should have been just stepDefinitions)

    2) I was using Spring 3 instead of Spring 4 with Cucumber 1.2.2 - The latest Cucumber requires Spring 4.

    The other stuff wasn't actually related to Spring and Cucumber.