-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of JUnit5 integration (#220)
- Loading branch information
Jan Schäfer
committed
Dec 26, 2016
1 parent
94fce51
commit a34f92c
Showing
36 changed files
with
802 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,5 @@ include::spring.adoc[] | |
include::selenium.adoc[] | ||
|
||
include::android.adoc[] | ||
|
||
include::junit5.adoc[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
== JUnit 5 (EXPERIMENTAL) | ||
:javadocurl: http://static.javadoc.io/com.tngtech.jgiven/jgiven-junit5/{version}/com/tngtech/jgiven/junit5 | ||
|
||
CAUTION: JUnit 5 support is currently in an experimental state. | ||
This means that the API is not completely stable yet and that not all | ||
JGiven and JUnit 5 features are completely supported yet. | ||
|
||
=== Known Issues | ||
|
||
* Nested tests are all reported under the outer parent test class | ||
* Dynamic tests are not reported at all. As dynamic tests in JUnit 5 do not provide life-cycle | ||
hooks, it is unclear at the moment, whether JGiven will ever support them. | ||
|
||
=== Install Dependency | ||
JUnit 5 support is provided by the `jgiven-junit5` dependency. | ||
|
||
==== Maven | ||
|
||
[source,maven,subs="verbatim,attributes"] | ||
---- | ||
<dependency> | ||
<groupId>com.tngtech.jgiven</groupId> | ||
<artifactId>jgiven-junit5</artifactId> | ||
<version>{version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
---- | ||
|
||
==== Gradle | ||
|
||
[source,gradle,subs="verbatim,attributes"] | ||
---- | ||
dependencies { | ||
testCompile("com.tngtech.jgiven:jgiven-junit5:{version}") | ||
} | ||
---- | ||
|
||
=== Use JGiven with JUnit 5 | ||
|
||
JGiven support for JUnit 5 is provided by the link:{javadocurl}/JGivenExtension.html[`JGivenExtension`] | ||
JUnit 5 extension. You just annotate your JUnit 5 test class with `@ExtendWith( JGivenExtension.class )` | ||
and JGiven is enabled. | ||
|
||
If you just use the extension directly, you have to inject your stage classes by using | ||
the `@ScenarioStage` annotation. | ||
|
||
[source,java] | ||
---- | ||
@ExtendWith( JGivenExtension.class ) | ||
public class JUnit5Test { | ||
@ScenarioStage | ||
MyStage myStage; | ||
@Test | ||
public void my_scenario() { | ||
myStage | ||
.given().some_context() | ||
.when().some_action() | ||
.then().some_outcome(); | ||
} | ||
} | ||
---- | ||
|
||
Alternatively, you can use one of the test base classes link:{javadocurl}/ScenarioTest.html[`ScenarioTest`] or | ||
link:{javadocurl}/SimpleScenarioTest.html[`SimpleScenarioTest`] that provide additional convenience methods | ||
and allows you to specify stage classes by using type parameters: | ||
|
||
[source,java] | ||
---- | ||
public class JGiven5ScenarioTest | ||
extends ScenarioTest<GivenStage, WhenStage, ThenStage> { | ||
@Test | ||
public void JGiven_works_with_JUnit5() { | ||
given().some_state(); | ||
when().some_action(); | ||
then().some_outcome(); | ||
} | ||
} | ||
---- | ||
|
||
=== Example Project | ||
|
||
You find a complete example project on GitHub: https://github.com/TNG/JGiven/tree/master/example-projects/junit5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# JUnit 5 Example Project | ||
|
||
This project shows how JGiven can be used with JUnit 5 | ||
|
||
1. Run `gradle build` | ||
2. Open `build/reports/jgiven/test/html/index.html` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
buildscript { | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3' | ||
} | ||
} | ||
|
||
plugins { | ||
id "com.tngtech.jgiven.gradle-plugin" version "0.13.0" | ||
} | ||
|
||
apply plugin: 'org.junit.platform.gradle.plugin' | ||
|
||
apply plugin: 'java' | ||
apply plugin: 'idea' | ||
apply plugin: 'eclipse' | ||
|
||
compileJava { | ||
sourceCompatibility = 1.8 | ||
targetCompatibility = 1.8 | ||
options.compilerArgs += '-parameters' | ||
options.encoding = 'UTF-8' | ||
} | ||
|
||
ext { | ||
junit5Version = '5.0.0-M3' | ||
} | ||
|
||
repositories { | ||
mavenLocal() | ||
mavenCentral() | ||
maven { | ||
url "https://oss.sonatype.org/content/repositories/snapshots/" | ||
} | ||
} | ||
|
||
dependencies { | ||
testCompile 'com.tngtech.jgiven:jgiven-junit5:latest.integration' | ||
testCompile 'org.junit.jupiter:junit-jupiter-api:' + junit5Version | ||
testCompile 'org.junit.jupiter:junit-jupiter-engine:' + junit5Version | ||
} | ||
|
||
junitPlatform { | ||
enableStandardTestTask true | ||
platformVersion = "1.0.0-M3" | ||
} | ||
|
||
test.finalizedBy jgivenTestReport |
16 changes: 16 additions & 0 deletions
16
...-projects/junit5/src/test/java/com/tngtech/jgiven/example/projects/junit5/GivenStage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.tngtech.jgiven.example.projects.junit5; | ||
|
||
import com.tngtech.jgiven.Stage; | ||
import com.tngtech.jgiven.annotation.Quoted; | ||
import com.tngtech.jgiven.annotation.ScenarioState; | ||
|
||
public class GivenStage extends Stage<GivenStage> { | ||
|
||
@ScenarioState | ||
String message; | ||
|
||
GivenStage message(@Quoted String message) { | ||
this.message = message; | ||
return self(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...-projects/junit5/src/test/java/com/tngtech/jgiven/example/projects/junit5/JUnit5Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.tngtech.jgiven.example.projects.junit5; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import com.tngtech.jgiven.annotation.ScenarioStage; | ||
import com.tngtech.jgiven.junit5.JGivenExtension; | ||
|
||
@ExtendWith( JGivenExtension.class ) | ||
public class JUnit5Test { | ||
|
||
@ScenarioStage | ||
GivenStage givenStage; | ||
|
||
@ScenarioStage | ||
WhenStage whenStage; | ||
|
||
@ScenarioStage | ||
ThenStage thenStage; | ||
|
||
@Test | ||
public void scenario_with_JUnit5() { | ||
givenStage.given().message( "Hello JUnit" ); | ||
whenStage.when().handle_message(); | ||
thenStage.then().the_result_is( "Hello JUnit 5!" ); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...e-projects/junit5/src/test/java/com/tngtech/jgiven/example/projects/junit5/ThenStage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.tngtech.jgiven.example.projects.junit5; | ||
|
||
import com.tngtech.jgiven.Stage; | ||
import com.tngtech.jgiven.annotation.ScenarioState; | ||
import org.junit.jupiter.api.Assertions; | ||
|
||
public class ThenStage extends Stage<ThenStage> { | ||
@ScenarioState(required = true) | ||
String result; | ||
|
||
public void the_result_is(String expectedResult) { | ||
Assertions.assertEquals(expectedResult, result); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...e-projects/junit5/src/test/java/com/tngtech/jgiven/example/projects/junit5/WhenStage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.tngtech.jgiven.example.projects.junit5; | ||
|
||
import com.tngtech.jgiven.Stage; | ||
import com.tngtech.jgiven.annotation.ScenarioState; | ||
|
||
public class WhenStage extends Stage<WhenStage> { | ||
@ScenarioState(required = true) | ||
String message; | ||
|
||
@ScenarioState | ||
String result; | ||
|
||
public void handle_message() { | ||
result = message + " 5!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
...tngtech/jgiven/testng/ScenarioHolder.java → ...m/tngtech/jgiven/impl/ScenarioHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.