黄瓜春天没有找到步骤定义

时间:2015-02-11 23:18:57

标签: spring junit cucumber-jvm

我需要帮助!

所以以下代码适用于我(Pure JUnit代码)

@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");
}
}

但是,当我尝试使用Cucumber功能运行此代码时,似乎无法正确构建。我假设我正在设置错误的项目。

这是我的步骤代码:

@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*/}


}

这是我的cucumber.xml文件:

<?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>

最后这是我的Runner Class:

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

如果有人可以请你告诉我为什么JUnit跑步者会工作而黄瓜一个人没有,我会是一个非常开心的露营者!

2 个答案:

答案 0 :(得分:3)

在上面的代码中,我做了一些错误,但让我们覆盖了大的。

1)我的胶水代码字符串不正确,我需要传递包名,而不是文件名(应该只是stepDefinitions)

2)我使用Spring 3而不是Spring 4和Cucumber 1.2.2 - 最新的Cucumber需要Spring 4。

其他东西实际上与Spring和Cucumber无关。

答案 1 :(得分:1)

步骤定义应该是步骤定义类的实例方法,而不是类(静态)方法。

为每个方案实例化(按需)步骤定义类,因此不应在方案之间泄露任何状态。