@@ -0,0 +1,124 @@
package net.thucydides.jbehave;

import net.thucydides.core.model.TestOutcome;
import net.thucydides.core.model.TestResult;
import net.thucydides.core.model.TestStep;
import org.jbehave.core.failures.FailureStrategy;
import org.jbehave.core.failures.PendingStepStrategy;
import org.junit.Test;

import java.util.List;

import static net.thucydides.core.matchers.PublicThucydidesMatchers.containsResults;
import static net.thucydides.core.model.TestResult.FAILURE;
import static net.thucydides.core.model.TestResult.SKIPPED;
import static net.thucydides.core.model.TestResult.SUCCESS;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class WhenRunningDataDrivenJBehaveStories extends AbstractJBehaveStory {

@Test
public void a_data_driven_test_should_produce_a_set_of_steps_per_line_of_data() throws Throwable {

// Given
JUnitThucydidesStories story = new AStorySample("aDataDrivenBehavior.story");

story.setSystemConfiguration(systemConfiguration);
story.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(story);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();
assertThat(outcomes.size(), is(1));
assertThat(outcomes.get(0).getTestSteps().size(), is(9));
}

@Test
public void a_data_driven_test_should_produce_a_steps_with_the_data_values_in_the_titles() throws Throwable {

// Given
JUnitThucydidesStories story = new AStorySample("aDataDrivenBehavior.story");

story.setSystemConfiguration(systemConfiguration);
story.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(story);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();
List<TestStep> steps = outcomes.get(0).getTestSteps();
assertThat(steps.get(0).getDescription(), is("Given a stock of ⦅STK1⦆ and a threshold of ⦅10.0⦆"));
assertThat(steps.get(3).getDescription(), is("Given a stock of ⦅STK1⦆ and a threshold of ⦅11.0⦆"));
assertThat(steps.get(6).getDescription(), is("Given a stock of ⦅STK1⦆ and a threshold of ⦅12.0⦆"));
}

@Test
public void a_data_driven_test_should_produce_a_successful_result_if_all_rows_are_successful() throws Throwable {

// Given
JUnitThucydidesStories story = new AStorySample("aDataDrivenBehavior.story");

story.setSystemConfiguration(systemConfiguration);
story.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(story);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();
assertThat(outcomes.get(0).getResult(), is(TestResult.SUCCESS));
}

@Test
public void a_failing_step_in_a_data_driven_test_should_not_affect_subsequent_steps() throws Throwable {

// Given
JUnitThucydidesStories story = new AStorySample("aFailingDataDrivenBehavior.story");

story.setSystemConfiguration(systemConfiguration);
story.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(story);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();

List<TestStep> steps = outcomes.get(0).getTestSteps();
assertThat(steps.get(2).getResult(), is(TestResult.SUCCESS));
assertThat(steps.get(6).getResult(), is(TestResult.FAILURE));
assertThat(steps.get(10).getResult(), is(TestResult.SUCCESS));

assertThat(outcomes.get(0).getResult(), is(TestResult.FAILURE));
}

@Test
public void steps_in_an_example_after_a_failing_step_should_be_skipped() throws Throwable {

// Given
JUnitThucydidesStories story = new AStorySample("aFailingDataDrivenBehavior.story");

story.setSystemConfiguration(systemConfiguration);
story.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(story);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();

List<TestStep> steps = outcomes.get(0).getTestSteps();
assertThat(steps.get(2).getResult(), is(TestResult.SUCCESS));
assertThat(steps.get(6).getResult(), is(TestResult.FAILURE));
assertThat(steps.get(7).getResult(), is(TestResult.SKIPPED));
assertThat(steps.get(10).getResult(), is(TestResult.SUCCESS));

assertThat(outcomes.get(0).getResult(), is(TestResult.FAILURE));

}

}
@@ -10,11 +10,13 @@
import static net.thucydides.core.matchers.PublicThucydidesMatchers.containsResults;
import static net.thucydides.core.model.TestResult.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;

public class WhenRunningJBehaveStories extends AbstractJBehaveStory {

private static final int TOTAL_NUMBER_OF_JBEHAVE_SCENARIOS = 10;
private static final int TOTAL_NUMBER_OF_JBEHAVE_SCENARIOS = 15;

final static class AllStoriesSample extends JUnitThucydidesStories {}

@@ -216,7 +218,6 @@ public void steps_after_a_failing_step_should_be_skipped() throws Throwable {

// And
assertThat(outcomes.get(0), containsResults(SUCCESS, FAILURE, SKIPPED, SKIPPED, SKIPPED));

}

@Test
@@ -242,4 +243,77 @@ public void a_test_with_a_pending_step_should_be_pending() throws Throwable {

}

@Test
public void a_test_should_be_associated_with_a_corresponding_issue_if_specified() throws Throwable {

// Given
JUnitThucydidesStories story = new AStorySample("aBehaviorWithAnIssue.story");

story.setSystemConfiguration(systemConfiguration);
story.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(story);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();
assertThat(outcomes.size(), is(1));
assertThat(outcomes.get(0).getIssueKeys(), hasItem("MYPROJ-456"));

}

@Test
public void a_test_can_be_associated_with_several_issues() throws Throwable {

// Given
JUnitThucydidesStories story = new AStorySample("aBehaviorWithMultipleIssues.story");

story.setSystemConfiguration(systemConfiguration);
story.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(story);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();
assertThat(outcomes.get(0).getIssueKeys(), hasItems("MYPROJ-3","MYPROJ-4","MYPROJ-5"));

}

@Test
public void a_test_story_can_be_associated_with_several_issues() throws Throwable {

// Given
JUnitThucydidesStories story = new AStorySample("aBehaviorWithMultipleIssues.story");

story.setSystemConfiguration(systemConfiguration);
story.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(story);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();
assertThat(outcomes.get(0).getIssueKeys(), hasItems("MYPROJ-1","MYPROJ-2","MYPROJ-3","MYPROJ-4","MYPROJ-5"));

}
@Test
public void all_the_scenarios_in_a_story_should_be_associated_with_a_corresponding_issue_if_specified_at_the_story_level() throws Throwable {

// Given
JUnitThucydidesStories story = new AStorySample("aBehaviorWithIssues.story");

story.setSystemConfiguration(systemConfiguration);
story.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(story);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();
assertThat(outcomes.size(), is(2));
assertThat(outcomes.get(0).getIssueKeys(), hasItems("MYPROJ-123", "MYPROJ-456"));
assertThat(outcomes.get(1).getIssueKeys(), hasItems("MYPROJ-123", "MYPROJ-789"));
}

}

This file was deleted.

@@ -1,6 +1,7 @@
package net.thucydides.jbehave.steps;

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Pending;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
@@ -46,4 +47,25 @@ public void andIShouldSkipSubsequentResults() {}
public void aJBehaveStoryWithAPendingImplementation() {}


@Given("a stock of <symbol> and a threshold of <threshold>")
public void givenAStock(@Named("symbol") String symbol, @Named("threshold") double threshold) {
System.out.println("Stock " + symbol);
}

@When("the stock is traded at <price>")
public void whenTheStockIsTradedAtprice(@Named("price") String price) {
System.out.println("Stock traded at: " + price);
}

@Then("the alert status should be <status>")
public void thenTheAlertStatusShouldBestatus(@Named("status") String status) {
System.out.println("Expected status: " + status);
if (status.equals("FAIL")) {
throw new AssertionError();
}
}

@Then("some other stuff should also work")
public void someOtherStuffShouldWork() {}
}

@@ -2,6 +2,7 @@

import net.thucydides.core.annotations.Steps;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Pending;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
@@ -0,0 +1,12 @@
As a: User
I want: to do something
So that: I can get payed

Scenario: A scenario that works
Meta:
@issue MYPROJ-456

Given I have an implemented JBehave scenario
And the scenario works
When I run the scenario
Then I should get a successful result
@@ -0,0 +1,20 @@
Meta:
@issue MYPROJ-123

Scenario: A scenario that works
Meta:
@issue MYPROJ-456

Given I have an implemented JBehave scenario
And the scenario works
When I run the scenario
Then I should get a successful result

Scenario: Another scenario that works
Meta:
@issue MYPROJ-789

Given I have an implemented JBehave scenario
And the scenario works
When I run the scenario
Then I should get a successful result
@@ -0,0 +1,21 @@
Meta:
@issue MYPROJ-1, MYPROJ-2

Scenario: A scenario that works
Meta:
@issues MYPROJ-3,MYPROJ-4
@issue MYPROJ-5

Given I have an implemented JBehave scenario
And the scenario works
When I run the scenario
Then I should get a successful result

Scenario: Another scenario that works
Meta:
@issues MYPROJ-6,MYPROJ-7

Given I have an implemented JBehave scenario
And the scenario works
When I run the scenario
Then I should get a successful result
@@ -0,0 +1,9 @@
Given a stock of <symbol> and a threshold of <threshold>
When the stock is traded at <price>
Then the alert status should be <status>

Examples:
|symbol|threshold|price|status|
|STK1|10.0|5.0|OFF|
|STK1|11.0|11.0|ON|
|STK1|12.0|12.0|ON|
@@ -0,0 +1,11 @@
package stories
Given a stock of <symbol> and a threshold of <threshold>
When the stock is traded at <price>
Then the alert status should be <status>
And some other stuff should also work

Examples:
|symbol|threshold|price|status|
|STK2|10.0|5.0|OFF|
|STK2|11.0|11.0|FAIL|
|STK2|12.0|12.0|ON|
@@ -65,12 +65,19 @@ public void testRunFinished(Result result) throws Exception {
*/
@Override
public void testStarted(final Description description) {
startTestSuiteForFirstTest(description);
StepEventBus.getEventBus().clear();
StepEventBus.getEventBus().testStarted(description.getMethodName(),
description.getTestClass());
startTest();
}

private void startTestSuiteForFirstTest(Description description) {
if (!getBaseStepListener().testSuiteRunning()) {
StepEventBus.getEventBus().testSuiteStarted(description.getTestClass());
}
}

@Override
public void testFinished(final Description description) throws Exception {
StepEventBus.getEventBus().testFinished();
@@ -201,6 +201,7 @@ public void useQualifier(final String qualifier) {
/**
* Runs the tests in the acceptance test case.
*/

@Override
public void run(final RunNotifier notifier) {
if (!skipThisTest()) {