Skip to content

Commit

Permalink
Initial implementation of JUnit5 integration (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Schäfer committed Dec 25, 2016
1 parent 94fce51 commit b862533
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
45 changes: 45 additions & 0 deletions jgiven-junit5/build.gradle
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')
compile project(':jgiven-html5-report')

compileOnly 'org.junit.jupiter:junit-jupiter-api:5.0.0-M3'

testCompile 'org.junit.jupiter:junit-jupiter-engine:5.0.0-M3'
}

copyJGivenReports.finalizedBy(jgivenHtml5Report)
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);
}

}
}
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();
}
}

}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include ':jgiven-core'
include ':jgiven-maven-plugin'
include ':jgiven-gradle-plugin'
include ':jgiven-junit'
include ':jgiven-junit5'
include ':jgiven-testng'
include ':jgiven-spring'
include ':jgiven-html5-report'
Expand Down

0 comments on commit b862533

Please sign in to comment.