Cucumber-spring 未找到步骤定义
Cucumber-spring is not finding Step Definitions
我需要帮助!
所以下面的代码对我有用(纯 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 运行ner 可以工作而黄瓜不能工作,我会很高兴!
步骤定义应该是步骤定义 classes 上的实例方法,而不是 class(静态)方法。
步骤定义class每个场景都实例化(按需),因此场景之间不应泄漏任何状态。
在上面的代码中,我做错了一些事情,但让我们来解决大问题。
1)我的Glue代码字符串不正确,我需要传入包名,而不是文件名(应该只是stepDefinitions)
2) 我在 Cucumber 1.2.2 中使用 Spring 3 而不是 Spring 4 - 最新的 Cucumber 需要 Spring 4.
其他内容实际上与 Spring 和 Cucumber 无关。
我需要帮助!
所以下面的代码对我有用(纯 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 运行ner 可以工作而黄瓜不能工作,我会很高兴!
步骤定义应该是步骤定义 classes 上的实例方法,而不是 class(静态)方法。
步骤定义class每个场景都实例化(按需),因此场景之间不应泄漏任何状态。
在上面的代码中,我做错了一些事情,但让我们来解决大问题。
1)我的Glue代码字符串不正确,我需要传入包名,而不是文件名(应该只是stepDefinitions)
2) 我在 Cucumber 1.2.2 中使用 Spring 3 而不是 Spring 4 - 最新的 Cucumber 需要 Spring 4.
其他内容实际上与 Spring 和 Cucumber 无关。