Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JGiven by just using JUnit rules #232

Merged
merged 1 commit into from
Sep 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## New Features

* Added possibility to use JGiven in JUnit by just using two rules. No deriving from ScenarioTest is necessary anymore
* Allow multiple formatter annotations on arguments, e.g., "@Quoted @YesNo", see [#204](https://github.com/TNG/JGiven/issues/204).
* Added a new comment() method to provide further information on specific step method invocations, see [#50](https://github.com/TNG/JGiven/issues/50).
* Steps can now have multiple attachments [#194](https://github.com/TNG/JGiven/issues/194).
Expand Down
25 changes: 23 additions & 2 deletions docs/getting_started.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ First of all you create a JUnit test class that inherits from `com.tngtech.jgive

[source]
----
include::{sourcedir}/JGivenTemplateTest.java[tags=noPackage]
include::{sourcedir}/MyShinyJGivenTest.java[tags=header]
}
----

The `ScenarioTest` requires 3 type parameters. Each of these type parameters represents a stage of the Given-When-Then notation. Note that there is also the `SimpleScenarioTest` class that only requires a single type parameter. In that case, all your scenario steps are defined in a single class.
Expand Down Expand Up @@ -52,4 +53,24 @@ The scenario is then executed like any other JUnit test, for example, by using y
[source,bash]
----
$ mvn test
----
----

=== Using JUnit Rules directly instead of deriving from ScenarioTest
Sometimes it is not possible to derive your test class from the `ScenarioTest` class, because you might already have a common base class that you cannot easily modify.
In that case you can directly use the JUnit Rules of JGiven. Instead of providing your stage classes as type parameters, you inject the stages into the test class with the `@ScenarioStage` annotation.

[source,java]
----
include::{sourcedir}/UsingRulesTest.java[tags=noPackage]
----

Note that your stage classes have to inherit from the `Stage` class in order to have the `given(), when()`, and `then()` methods.
You can now also define convenient methods in your test class to get cleaner scenarios if you like:

[source,java]
----
public GivenSomeState given() {
return someStage.given();
}
----

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.tngtech.jgiven.examples.userguide;

import com.tngtech.jgiven.annotation.ScenarioStage;
import com.tngtech.jgiven.junit.ScenarioExecutionRule;
import com.tngtech.jgiven.junit.ScenarioReportRule;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

//tag::noPackage[]

public class UsingRulesTest {

@ClassRule
public static final ScenarioReportRule writerRule = new ScenarioReportRule();

@Rule
public final ScenarioExecutionRule scenarioRule = new ScenarioExecutionRule();

@ScenarioStage
GivenSomeState someState;

@ScenarioStage
WhenSomeAction someAction;

@ScenarioStage
ThenSomeOutcome someOutcome;

@Test
public void something_should_happen() {
someState.given().some_state();
someAction.when().some_action();
someOutcome.then().some_outcome();
}
}
//end::noPackage[]
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,27 @@ public ScenarioExecutionRule( Object testInstance, ScenarioBase scenario ) {
this.scenario = scenario;
}

/**
* @since 0.12.0
*/
public ScenarioExecutionRule() {
this.scenario = new ScenarioBase();
}

/**
* @since 0.8.1
*/
public ScenarioExecutionRule( ScenarioBase scenario ) {
this.scenario = scenario;
}

/**
* Returns the ScenarioBase instance of this rule
*/
public ScenarioBase getScenario() {
return scenario;
}

@Override
public Statement apply( final Statement base, final FrameworkMethod method, final Object target ) {
return new Statement() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tngtech.jgiven.junit;

import com.tngtech.jgiven.annotation.ScenarioStage;
import org.junit.ClassRule;
import org.junit.Rule;

import com.tngtech.jgiven.Stage;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Verifies that JGiven can be used without inheriting from any class,
* just by using the two JGiven rules
*/
public class OnlyRulesTest {

@ClassRule
public static final ScenarioReportRule writerRule = new ScenarioReportRule();

@Rule
public final ScenarioExecutionRule scenarioRule = new ScenarioExecutionRule();

@ScenarioStage
TestStage stage;

@Test
public void JGiven_can_be_used_just_by_using_JUnit_rules() {
stage.given().something();

assertThat(scenarioRule.getScenario().getScenarioModel().getCase(0).getFirstStep().getLastWord().getValue()).isEqualTo("something");
}

public static class TestStage extends Stage<TestStage> {
public void something() {

}
}

}