黄瓜JUNIT步骤执行被忽略

时间:2013-07-12 02:22:01

标签: cucumber

我一直在拼命尝试解决黄瓜Junit步骤的执行问题。

我只是按照一个简单的例子来定义一个功能,测试和步骤如下:

Feature: Campaign Budget Calculation

Scenario: Valid Input Parameters
  Given campaign budget as 100 and campaign amount spent as 120
  When the campaign budget is less than campaign amount spent
  Then throw an Error

测试:

@RunWith(Cucumber.class)
@Cucumber.Options(glue = { "com.reachlocal.opt.dbas" })
public class CampaignTest {

}

步骤:

public class CampaignTestStepDefinitions {

    private Campaign campaign;

    @Given("^a campaign with (\\d+) of budget and (\\d+) of amount spent$")
    public void createCampaign(int arg1, int arg2) throws Throwable{
        CurrencyUnit usd = CurrencyUnit.of("USD");
        campaign = new Campaign();
        campaign.setCampaignBudget(Money.of(usd, arg1));
        campaign.setCampaignAmountSpent(Money.of(usd, arg2));
    }

    @When("^compare the budget and the amount spent$")
    public void checkCampaignBudget() throws Throwable{
        if (campaign.getCampaignBudget().isLessThan(campaign.getCampaignAmountSpent())) {
            campaign.setExceptionFlag(new Boolean(false));
        }
    }

    @Then("^check campaign exception$")
    public void checkCampaignException() throws Throwable{
        if (campaign.getExceptionFlag()) {
            assertEquals(new Boolean(true), campaign.getExceptionFlag());
        }
    }
}

当我运行junit时,会跳过这些步骤,结果显示它们都被忽略了。我以前尝试过没有胶水,但没有帮助。 不知道为什么。来自互联网的简单示例代码,如添加2个数字,工作正常。 我正在使用STS在Maven / Spring项目中运行它。

3 个答案:

答案 0 :(得分:2)

@ Given,@ When和@Then表达式与功能文件不匹配。它们是需要匹配要素文件中的线条的正则表达式。

例如,对于要素线:

  

广告系列预算为100,广告系列金额为120

在您已获得的步骤文件中:

  

@Given(" ^具有预算(+ d +)和(\ d +)金额的广告系列   花费$&#34)

但它应该是:

  

@Given(" ^广告系列预算为(\ d +),广告系列金额为   (\ d +)$&#34)

然后它应该匹配而不是忽略该步骤。

刚刚遇到同样的问题,在Eclipse中很容易错过,因为虽然它确实说它们被忽略了但你仍然会得到一个绿色标记。

答案 1 :(得分:0)

试试这个,

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(format = { "json:target/REPORT_NAME.json", "pretty",
    "html:target/HTML_REPORT_NAME" }, features = { "src/test/resources/PATH_TO_FEATURE_FILE/NAME_OF_FEATURE.feature" })
public class Run_Cukes_Test {

}

这一直对我有用。

答案 2 :(得分:0)

我也遇到了同样的错误,结果是步骤定义中的所有方法都是私有而不是公开

相关问题