-
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 67f520e
Showing
11 changed files
with
295 additions
and
2 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 |
---|---|---|
@@ -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() | ||
// Currently JUnit 5 seems to have problems when having the | ||
// using the snapshot repository | ||
// maven { | ||
// url "https://oss.sonatype.org/content/groups/public/" | ||
//} | ||
} | ||
|
||
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 | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
description = "Module for writing JGiven tests with JUnit 5" | ||
|
||
buildscript { | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3' | ||
} | ||
} | ||
|
||
apply plugin: 'org.junit.platform.gradle.plugin' | ||
|
||
compileJava { | ||
sourceCompatibility = 1.8 | ||
targetCompatibility = 1.8 | ||
options.compilerArgs += '-parameters' | ||
options.encoding = 'UTF-8' | ||
} | ||
|
||
task cleanReports(type: Delete) { | ||
delete('build/reports/jgiven/json') | ||
} | ||
|
||
task copyJGivenReports(type: Copy, dependsOn: cleanReports) { | ||
from('jgiven-reports') | ||
into('build/reports/jgiven/json') | ||
} | ||
test.finalizedBy(copyJGivenReports) | ||
|
||
junitPlatform { | ||
enableStandardTestTask true | ||
} | ||
|
||
|
||
dependencies { | ||
compile project(':jgiven-core') | ||
|
||
compileOnly 'org.junit.jupiter:junit-jupiter-api:5.0.0-M3' | ||
|
||
testCompile project(':jgiven-html5-report') | ||
testCompile 'org.junit.jupiter:junit-jupiter-engine:5.0.0-M3' | ||
} | ||
|
||
copyJGivenReports.finalizedBy(jgivenHtml5Report) |
72 changes: 72 additions & 0 deletions
72
jgiven-junit5/src/main/java/com/tngtech/jgiven/junit5/JGivenExtension.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,72 @@ | ||
package com.tngtech.jgiven.junit5; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ContainerExtensionContext; | ||
import org.junit.jupiter.api.extension.TestExtensionContext; | ||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; | ||
|
||
import com.tngtech.jgiven.impl.ScenarioBase; | ||
import com.tngtech.jgiven.report.impl.CommonReportHelper; | ||
import com.tngtech.jgiven.report.model.NamedArgument; | ||
import com.tngtech.jgiven.report.model.ReportModel; | ||
|
||
public class JGivenExtension implements | ||
BeforeAllCallback, | ||
AfterAllCallback, | ||
BeforeEachCallback, | ||
AfterEachCallback { | ||
|
||
public static final Namespace NAMESPACE = Namespace.create( "com.tngtech.jgiven" ); | ||
|
||
private static final String REPORT_MODEL = "report-model"; | ||
private static final String SCENARIO = "scenario"; | ||
|
||
@Override | ||
public void beforeAll( ContainerExtensionContext context ) throws Exception { | ||
ReportModel reportModel = new ReportModel(); | ||
reportModel.setTestClass( context.getTestClass().get() ); | ||
context.getStore( NAMESPACE ).put( REPORT_MODEL, reportModel ); | ||
} | ||
|
||
@Override | ||
public void afterAll( ContainerExtensionContext context ) throws Exception { | ||
new CommonReportHelper().finishReport( (ReportModel) context.getStore( NAMESPACE ).get( REPORT_MODEL ) ); | ||
} | ||
|
||
@Override | ||
public void beforeEach( TestExtensionContext context ) throws Exception { | ||
ScenarioBase scenario = new ScenarioBase(); | ||
context.getStore( NAMESPACE ).put( SCENARIO, scenario ); | ||
|
||
ReportModel reportModel = (ReportModel) context.getParent().get().getStore( NAMESPACE ).get( REPORT_MODEL ); | ||
scenario.setModel( reportModel ); | ||
scenario.getExecutor().injectStages( context.getTestInstance() ); | ||
|
||
List<NamedArgument> args = new ArrayList<NamedArgument>(); | ||
scenario.startScenario( context.getTestClass().get(), context.getTestMethod().get(), args ); | ||
|
||
scenario.getExecutor().readScenarioState( context.getTestInstance() ); | ||
} | ||
|
||
@Override | ||
public void afterEach( TestExtensionContext context ) throws Exception { | ||
ScenarioBase scenario = (ScenarioBase) context.getStore( NAMESPACE ).get( SCENARIO ); | ||
try { | ||
if( context.getTestException().isPresent() ) { | ||
scenario.getExecutor().failed( context.getTestException().get() ); | ||
} | ||
scenario.finished(); | ||
} catch( Exception e ) { | ||
throw e; | ||
} catch( Throwable e ) { | ||
throw new RuntimeException( e ); | ||
} | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/JGiven5Test.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,36 @@ | ||
package com.tngtech.jgiven.junit5.test; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import com.tngtech.jgiven.Stage; | ||
import com.tngtech.jgiven.annotation.ScenarioStage; | ||
import com.tngtech.jgiven.junit5.JGivenExtension; | ||
|
||
@ExtendWith( JGivenExtension.class ) | ||
public class JGiven5Test { | ||
|
||
@ScenarioStage | ||
Steps steps; | ||
|
||
@Test | ||
public void JGiven_works_with_JUnit5() { | ||
steps.given().some_action().when().some_context().then().some_outcome(); | ||
|
||
} | ||
|
||
public static class Steps extends Stage<Steps> { | ||
Steps some_action() { | ||
return self(); | ||
} | ||
|
||
Steps some_context() { | ||
return self(); | ||
} | ||
|
||
Steps some_outcome() { | ||
return self(); | ||
} | ||
} | ||
|
||
} |
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