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 26, 2016
1 parent 94fce51 commit 67f520e
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 2 deletions.
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,18 @@ configure(subprojects) {

}

configure(subprojects.findAll {!it.name.contains("jgiven-junit5")}) {
apply plugin: 'ru.vyarus.animalsniffer'

dependencies {
signature 'org.codehaus.mojo.signature:java16:1.1@signature'
}
}

configure(subprojects.findAll {!it.name.contains("android")}) {
apply plugin: 'checkstyle'
apply plugin: 'java'
apply plugin: 'org.asciidoctor.convert'
apply plugin: 'ru.vyarus.animalsniffer'

dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion
Expand All @@ -197,7 +204,6 @@ configure(subprojects.findAll {!it.name.contains("android")}) {
testCompile group: 'org.assertj', name: 'assertj-core', version: assertjVersion
testCompile group: 'com.tngtech.java', name: 'junit-dataprovider', version: junitDataproviderVersion
testCompile group: 'net.java.quickcheck', name: 'quickcheck', version: quickcheckVersion
signature 'org.codehaus.mojo.signature:java16:1.1@signature'
}

sourceCompatibility = targetCompatibility = 1.6
Expand Down
6 changes: 6 additions & 0 deletions example-projects/junit5/README.md
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`
51 changes: 51 additions & 0 deletions example-projects/junit5/build.gradle
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
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();
}
}
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!" );
}
}
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);
}
}
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!";
}
}
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')

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

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

}
4 changes: 4 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ if (JavaVersion.current().isJava8Compatible() && !release) {
include ':jgiven-java8-tests'
}

if (JavaVersion.current().isJava8Compatible()) {
include ':jgiven-junit5'
}

if (android || release) {
include ':jgiven-android'
}
Expand Down

0 comments on commit 67f520e

Please sign in to comment.