2015-02-11 71 views
2

我需要幫助!黃瓜春無法找到步定義

所以下面的代碼對我的作品(純的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"); 
} 
} 

然而,當我嘗試使用黃瓜功能來運行這段代碼,它似乎無法正確生成。我假設我正在設置錯誤的項目。

這裏是我的步驟代碼:

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

最後這裏是我的亞軍類:

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

如果有人能請啓發我爲什麼JUnit亞軍工作和黃瓜之一不會我會成爲一個非常快樂的露營者!

+0

@JasonI閱讀,你會得到構建錯誤在哪個行和什麼是構建錯誤消息? – erhun 2015-02-11 23:35:30

+0

@erhun我在這方面取得了一些進展 - 我錯過了我的步驟定義頂部的@ContextConfiguration(「classpath:cucumber.xml」)代碼。但是,現在我的功能認爲步驟代碼沒有實現 – 2015-02-11 23:46:31

+0

錯誤說「每一步」被忽略,並且「我可以實現提供的存根代碼的步驟」 – 2015-02-12 17:35:12

回答

3

在上面的代碼中,我做了一些錯誤的事情,但讓我們覆蓋大的。

1)我的膠水代碼串是不正確的,我需要在包名來傳遞,而不是文件名,我使用Spring 3來代替彈簧4的黃瓜(應該是剛剛stepDefinitions)

2) 1.2.2 - 最新的黃瓜要求春季4.

其他的東西實際上並沒有涉及春季和黃瓜。

1

步驟定義應該是步驟定義類的實例方法,而不是類(靜態)方法。

步驟定義類爲每個場景實例化(按需),所以沒有狀態應該在場景之間泄漏。

+0

我刪除了「靜態」部分 – 2015-02-12 21:12:50

+0

99%肯定這是我的黃瓜跑步者的問題... – 2015-02-12 21:35:04

+0

好吧,所以我對我的代碼做了一些調整,現在我得到一個「java.lang.IncompatibleClassChangeError:發現類org.springframework.test.context.TestContext,但接口是預期的「錯誤 – 2015-02-12 21:42:17