-
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 25, 2016
1 parent
94fce51
commit 04bffac
Showing
4 changed files
with
150 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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') | ||
compile project(':jgiven-html5-report') | ||
compile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M3' | ||
compile 'org.junit.jupiter:junit-jupiter-engine:5.0.0-M3' | ||
} | ||
|
||
copyJGivenReports.finalizedBy(jgivenHtml5Report) |
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
package com.tngtech.jgiven.junit5; | ||
|
||
import com.tngtech.jgiven.impl.Config; | ||
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; | ||
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.ExtendWith; | ||
import org.junit.jupiter.api.extension.TestExtensionContext; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class JGivenExtension implements | ||
BeforeAllCallback, | ||
AfterAllCallback, | ||
BeforeEachCallback, | ||
AfterEachCallback { | ||
|
||
public static final String REPORT_MODEL = "report-model"; | ||
public static final String SCENARIO = "scenario"; | ||
|
||
@Override | ||
public void beforeAll(ContainerExtensionContext context) throws Exception { | ||
ReportModel reportModel = new ReportModel(); | ||
reportModel.setTestClass(context.getTestClass().get()); | ||
context.getStore().put(REPORT_MODEL, reportModel); | ||
} | ||
|
||
@Override | ||
public void afterAll(ContainerExtensionContext context) throws Exception { | ||
new CommonReportHelper().finishReport((ReportModel) context.getStore().get(REPORT_MODEL)); | ||
} | ||
|
||
@Override | ||
public void beforeEach(TestExtensionContext context) throws Exception { | ||
ScenarioBase scenario = new ScenarioBase(); | ||
context.getStore().put(SCENARIO, scenario); | ||
|
||
ReportModel reportModel = (ReportModel) context.getParent().get().getStore().get(REPORT_MODEL); | ||
scenario.setModel( reportModel ); | ||
scenario.getExecutor().injectStages( context.getTestInstance() ); | ||
|
||
List<NamedArgument> args = new ArrayList<>(); | ||
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().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