Vimal
Vimal

Reputation: 191

Cucumber JUNIT Step Execution Ignored

I have been desperately trying to resolve a Cucumber Junit step execution.

I just followed a simple example of defining a feature, test and steps as below:

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

Test:

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

}

Steps:

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());
        }
    }
}

When I run the junit, the steps are skipped, and the results shows they are all ignored. I have tried without glue as well before but does not help. Not sure why. Simple Example code from the Internet like adding 2 numbers are working fine. I'm running it in Maven/Spring project using STS.

Upvotes: 1

Views: 9267

Answers (3)

vikramvi
vikramvi

Reputation: 3605

I also got same error and it turns out that all the methods under step definitions were private instead of public

Upvotes: 0

BenHT
BenHT

Reputation: 113

The @Given, @When and @Then expressions don't match the feature file. They're regular expressions that need to match lines in the feature file.

For example, for the feature line :

Given campaign budget as 100 and campaign amount spent as 120

in the steps file you've got :

@Given("^a campaign with (\d+) of budget and (\d+) of amount spent$")

but it should be :

@Given("^campaign budget as (\d+) and campaign amount spent as (\d+)$")

Then it should match and not ignore the step.

Just had the same issue, it's easy to miss in Eclipse because you still get a green tick though it does say they were ignored.

Upvotes: 2

LINGS
LINGS

Reputation: 3630

Try this,

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 {

}

This always has been working for me.

Upvotes: 0

Related Questions