Skip to content

Commit

Permalink
Merge branch 'master' of
Browse files Browse the repository at this point in the history
ssh://git@github.com/AndreasEK/jbehave-junit-monitor.git

Conflicts:
	src/test/java/org/jbehave/scenario/finegrained/junit/monitoring/step/ExampleSteps.java
  • Loading branch information
AndreasEK committed May 10, 2012
2 parents fde02aa + 4d8f235 commit 7b29f41
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 77 deletions.
9 changes: 6 additions & 3 deletions README.markdown
Expand Up @@ -45,6 +45,9 @@ listed here, too:
* junit (4.10)
* slf4j-api (1.6.4)

Note that the JBehave library is set to scope `provided`
in the POM, because any project using this Runner will
naturally have their own version of JBehave already.

Usage
====================================
Expand All @@ -54,7 +57,7 @@ of your JBehave acceptance tests.

Enabling
-------------------------------
You will probably alread have one or more classes
You will probably already have one or more classes
that extend the default JBehave `JUnitStories` class.
Just add this annotation:

Expand Down Expand Up @@ -86,10 +89,10 @@ however in case you pulled up the `storyPaths()` method
into that common base class, you

1. either need to make it public _OR_
1. override it in all your base classes
1. override it in all descendants of that base class
(just calling the super implementation is fine).

This is due to a reflection issue. This might be fixed in
This is due to a reflection issue and might be fixed in
future versions.


Expand Down
35 changes: 17 additions & 18 deletions pom.xml
@@ -1,15 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.codecentric</groupId>
<artifactId>jbehave-junit-runner</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-core</artifactId>
<version>3.6</version>
<type>jar</type>
<scope>compile</scope>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.codecentric</groupId>
<artifactId>jbehave-junit-runner</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<jbehave.version>3.6</jbehave.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-core</artifactId>
<version>${jbehave.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand All @@ -22,12 +27,6 @@
<artifactId>mockito-core</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-guice</artifactId>
<version>3.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
@@ -1,17 +1,10 @@
package org.jbehave.scenario.finegrained.junit.monitoring;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.jbehave.core.model.ExamplesTable;
import org.jbehave.core.model.GivenStories;
import org.jbehave.core.model.GivenStory;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Story;
import org.jbehave.core.steps.CandidateSteps;
Expand All @@ -24,12 +17,9 @@ public class JUnitDescriptionGenerator {

private int testCases;

private final List<CandidateSteps> candidateSteps;

private List<StepCandidate> allCandidates = new ArrayList<StepCandidate>();

public JUnitDescriptionGenerator(List<CandidateSteps> candidateSteps) {
this.candidateSteps = candidateSteps;
for (CandidateSteps candidateStep : candidateSteps) {
allCandidates.addAll(candidateStep.listCandidates());
}
Expand Down Expand Up @@ -86,8 +76,11 @@ private void addScenarioSteps(Scenario scenario, Description description) {
for (StepCandidate step : allCandidates) {
if (step.matches(stringStep)) {
// JUnit and the Eclipse JUnit view needs to be touched/fixed in order to make the JUnit view
// jump to the corresponding test accordingnly. For now we have to live, that we end up in
// jump to the corresponding test accordingly. For now we have to live, that we end up in
// the correct class.
if (stringStep.indexOf('\n') != -1) {
stringStep = stringStep.substring(0, stringStep.indexOf('\n'));
}
Description testDescription = Description.createTestDescription(step.getStepsInstance().getClass(), getJunitSafeString(stringStep));
description.addChild(testDescription);
}
Expand Down
@@ -1,49 +1,33 @@
package org.jbehave.scenario.finegrained.junit.monitoring;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.jbehave.core.Embeddable;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.i18n.LocalizedKeywords;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.model.ExamplesTableFactory;
import org.jbehave.core.parsers.RegexStoryParser;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.jbehave.core.steps.ParameterConverters;
import org.jbehave.core.steps.ParameterConverters.DateConverter;
import org.jbehave.core.steps.ParameterConverters.ExamplesTableConverter;
import org.jbehave.scenario.finegrained.junit.monitoring.step.ExampleSteps;
import org.junit.runner.RunWith;

//@RunWith(JUnitReportingRunner.class)

@RunWith(JUnitReportingRunner.class)
public class ExampleScenario extends JUnitStories {

public ExampleScenario() {
useStepsFactory(new InstanceStepsFactory(configuration(), new ExampleSteps()));
configuredEmbedder().embedderControls().useThreads(1);

}

@Override
public InjectableStepsFactory stepsFactory() {
return new InstanceStepsFactory(configuration(), new ExampleSteps());
}

@Override
public Configuration configuration() {
Class<? extends Embeddable> embeddableClass = this.getClass();
// Start from default ParameterConverters instance
ParameterConverters parameterConverters = new ParameterConverters();
// factory to allow parameter conversion and loading from external
// resources (used by StoryParser too)
ExamplesTableFactory examplesTableFactory = new ExamplesTableFactory(
new LocalizedKeywords(),
new LoadFromClasspath(embeddableClass), parameterConverters);
// add custom coverters
parameterConverters.addConverters(new DateConverter(
new SimpleDateFormat("yyyy-MM-dd")),
new ExamplesTableConverter(examplesTableFactory));
return new MostUsefulConfiguration()
.useStoryLoader(new LoadFromClasspath(embeddableClass))
.useStoryParser(new RegexStoryParser(examplesTableFactory))
.useParameterConverters(parameterConverters);
return new MostUsefulConfiguration();
}

@Override
Expand All @@ -52,7 +36,5 @@ protected List<String> storyPaths() {
stories.add("org/jbehave/scenario/finegrained/junit/monitoring/Multiplication.story");
return stories;
}



}
Expand Up @@ -13,7 +13,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -83,6 +82,14 @@ public void shouldGenerateDescriptionForStep() {
assertThat(description.getChildren(), hasItem(step1Description()));
assertThat(generator.getTestCases(), is(1));
}

@Test
public void shouldGenerateDescriptionForTabularParameterStep() {
when(scenario.getSteps()).thenReturn(Arrays.asList("StepWithTableParam:\n|Head|\n|Value|"));
Description description = generator.createDescriptionFrom(scenario);
assertThat(description.getChildren(), hasItem(stepWithTableDescription()));
assertThat(generator.getTestCases(), is(1));
}

private void addStepToScenario() {
when(scenario.getSteps()).thenReturn(Arrays.asList("Step1"));
Expand All @@ -91,6 +98,10 @@ private void addStepToScenario() {
private Description step1Description() {
return Description.createTestDescription(Object.class, "Step1");
}

private Description stepWithTableDescription() {
return Description.createTestDescription(Object.class, "StepWithTableParam:");
}

@Test
public void shouldGenerateDescriptionForStory() {
Expand Down
Expand Up @@ -4,13 +4,9 @@
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;

import org.hamcrest.BaseMatcher;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.mockito.Mock;
Expand Down
Expand Up @@ -11,6 +11,7 @@
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.model.ExamplesTable;
import org.jbehave.core.steps.Parameters;
import org.junit.Assert;

public class ExampleSteps {
Expand All @@ -28,6 +29,14 @@ public void whenImultiplyXBy(@Named("value") int value) {
x = x * value;
}

@When("I multiply x with all of:$param")
public void whenImultiplyXByOneOf(ExamplesTable param) {
for (Parameters p : param.getRowsAsParameters()) {
Integer value = p.valueAs("Value", Integer.class);
x = x * value;
}
}

@Then("x should equal $value")
public void thenXshouldBe(@Named("value") int value) {
Assert.assertEquals(value, x);
Expand Down
Expand Up @@ -7,12 +7,7 @@
import org.jbehave.core.annotations.BeforeScenario;
import org.jbehave.core.annotations.BeforeStories;
import org.jbehave.core.annotations.BeforeStory;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.ScenarioType;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.junit.Assert;

public class InitSteps {
@BeforeStories
Expand Down
Expand Up @@ -10,15 +10,10 @@
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.jbehave.core.steps.guice.GuiceStepsFactory;
import org.jbehave.scenario.finegrained.junit.monitoring.JUnitReportingRunner;
import org.jbehave.scenario.finegrained.junit.monitoring.step.ExampleSteps;
import org.junit.runner.RunWith;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Scopes;

/**
* <p>
* {@link Embeddable} class to run multiple textual stories via JUnit.
Expand Down
Expand Up @@ -20,4 +20,14 @@ Scenario: 6 multiplied by 2

Given a variable x with value 6
When I multiply x by 2
Then x should equal 12
Then x should equal 12

Scenario: 2 multiplied with multiple values

Given a variable x with value 2
When I multiply x with all of:
| Value |
| 3 |
| 4 |
| 5 |
Then x should equal 120

0 comments on commit 7b29f41

Please sign in to comment.