Skip to content

Commit

Permalink
Issue-373: Wrap up Scenario earlier in JUnit5 Lifecycle
Browse files Browse the repository at this point in the history
Also update JGiven Framework tests to also test Junit5

Signed-off-by: l-1sqared <[email protected]>
  • Loading branch information
l-1squared committed Feb 9, 2022
1 parent 3e40fd5 commit bc0a12b
Show file tree
Hide file tree
Showing 27 changed files with 476 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class JGivenExtension implements
BeforeAllCallback,
AfterAllCallback,
BeforeEachCallback,
AfterEachCallback {
AfterTestExecutionCallback {

private static final Namespace NAMESPACE = Namespace.create("com.tngtech.jgiven");

Expand Down Expand Up @@ -75,7 +75,7 @@ public void beforeEach(ExtensionContext context) {
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
public void afterTestExecution(ExtensionContext context) throws Exception {
ScenarioBase scenario = getScenario();
try {
if (context.getExecutionException().isPresent()) {
Expand Down
4 changes: 4 additions & 0 deletions jgiven-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ description = 'JGiven Tests - Contains BDD tests for JGiven written in JGiven'
dependencies {
implementation project(':jgiven-junit')
implementation project(':jgiven-testng')
implementation project(':jgiven-junit5')
implementation project(':jgiven-html5-report')
implementation "junit:junit:$junitVersion"
implementation "org.junit.jupiter:junit-jupiter-api:5.8.2"
implementation "org.junit.jupiter:junit-jupiter-engine:5.8.2"
implementation "org.junit.platform:junit-platform-runner:1.8.2"
implementation "org.testng:testng:$testngVersion"
implementation "com.tngtech.java:junit-dataprovider:$junitDataproviderVersion"
implementation 'com.beust:jcommander:1.82'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.tngtech.jgiven.tags;

import com.tngtech.jgiven.annotation.IsTag;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@FeatureTestFramework
@IsTag( name = "JUnit5",
description = "Tests can be be executed with JUnit5" )
@Retention( RetentionPolicy.RUNTIME )
public @interface FeatureJUnit5 {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import com.tngtech.jgiven.junit.ScenarioTest;
import org.junit.Test;

public class GuaranteedFieldRealTest extends ScenarioTest<GuaranteedFieldRealTest.RealGiven, GuaranteedFieldRealTest.RealWhen, GuaranteedFieldRealTest.RealThen> {
public class GuaranteedFieldRealTest extends
ScenarioTest<GuaranteedFieldRealTest.RealGiven, GuaranteedFieldRealTest.RealWhen, GuaranteedFieldRealTest.RealThen> {

@Test
public void a_sample_test() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.tngtech.jgiven.tests;

import com.tngtech.jgiven.exception.JGivenWrongUsageException;
import com.tngtech.jgiven.impl.ScenarioBase;
import com.tngtech.jgiven.impl.ScenarioHolder;
import com.tngtech.jgiven.junit5.JGivenExtension;
import com.tngtech.jgiven.report.model.ReportModel;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.jupiter.api.extension.ExtensionContext;

/**
* Extracts the report Model from within the Test context before it gets deleted.
*/
public class JGivenReportExtractingExtension extends JGivenExtension {

private static final Map<Class<?>, ReportModel> modelHolder = new ConcurrentHashMap<>();

public static Optional<ReportModel> getReportModelFor(Class<?> testClass) {
return Optional.ofNullable(modelHolder.get(testClass));
}


@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
Class<?> testClass = context.getTestClass()
.orElseThrow(() -> new JGivenWrongUsageException("tests without test class are not supported yet"));
Optional.ofNullable(ScenarioHolder.get())
.map(ScenarioHolder::getScenarioOfCurrentThread)
.map(ScenarioBase::getModel)
.ifPresent(model -> modelHolder.put(testClass, model));
super.afterTestExecution(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.tngtech.jgiven.tests;

import com.tngtech.jgiven.base.ScenarioTestBase;
import com.tngtech.jgiven.impl.Scenario;
import com.tngtech.jgiven.report.model.ScenarioCaseModel;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(JGivenReportExtractingExtension.class)
public class JUnit5AfterMethodTests extends ScenarioTestBase<GivenTestStage, WhenTestStage, ThenTestStage> {

private final Scenario<GivenTestStage, WhenTestStage, ThenTestStage> scenario = createScenario();

@Override
public Scenario<GivenTestStage, WhenTestStage, ThenTestStage> getScenario() {
return scenario;
}

@Test
public void a_failing_JUnit_5_test() {
given().nothing();
when().a_step_fails();
then().something_happened();
}

@Test
public void a_succeding_JUnit5_test() {
given().nothing();
when().something_happens();
then().something_happened();
}

@Test
@Disabled
public void a_skipped_JUnit5_test() {
given().nothing();
when().something_happens();
then().something_happened();
}


@AfterEach
public void modifyScenario() {
getScenario().getModel().getLastScenarioModel().addCase(new ScenarioCaseModel());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.tngtech.jgiven.impl.Scenario;
import com.tngtech.jgiven.junit.ScenarioTest;
import com.tngtech.jgiven.impl.ScenarioHolder;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(JGivenReportExtractingExtension.class)
public class ScenarioTestForTesting<GIVEN, WHEN, THEN> extends ScenarioTest<GIVEN,WHEN,THEN> {
@Override
public Scenario<GIVEN, WHEN, THEN> getScenario() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.tngtech.jgiven.tests;

import org.junit.Test;
import org.testng.annotations.Listeners;

import com.tngtech.jgiven.annotation.Description;
import com.tngtech.jgiven.junit.ScenarioTest;
import com.tngtech.jgiven.testng.ScenarioTestListener;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testng.annotations.Listeners;

@Listeners( ScenarioTestListener.class )
@ExtendWith(JGivenReportExtractingExtension.class)
@Description( "Test Description" )
public class TestClassWithDescription extends ScenarioTest<GivenTestStage, WhenTestStage, ThenTestStage> {

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
public void some_test() {
given().nothing();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.tngtech.jgiven.tests;

import com.tngtech.jgiven.testng.ScenarioTestListener;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testng.annotations.Listeners;

import com.tngtech.jgiven.junit.ScenarioTest;
import com.tngtech.jgiven.testng.ScenarioTestListener;

@Listeners( ScenarioTestListener.class )
@ExtendWith(JGivenReportExtractingExtension.class)
public class TestClassWithOnlyIgnoredTests extends ScenarioTestForTesting<GivenTestStage, WhenTestStage, ThenTestStage> {

@Ignore
@Test
@org.junit.jupiter.api.Test
@Disabled
@org.testng.annotations.Test( enabled = false )
public void test() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ public static TestScenario testWithTwoCasesAndTheFirstOneFails() {
return new TestScenario( TestWithTwoCasesAndAFailingOne.class );
}

public static TestScenario junit5TestsWithModificationsInAfterMethod(){
return new TestScenario(JUnit5AfterMethodTests.class);
}

public static TestScenario testNgTestWithAFailingCase() {
return new TestScenario( FailingCasesTestNgTest.class );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package com.tngtech.jgiven.tests;

import org.junit.Test;
import org.testng.annotations.Listeners;

import com.tngtech.jgiven.annotation.Pending;
import com.tngtech.jgiven.testng.ScenarioTestListener;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testng.annotations.Listeners;

@Listeners( ScenarioTestListener.class )
@ExtendWith(JGivenReportExtractingExtension.class)
public class TestScenarios extends ScenarioTestForTesting<GivenTestStage, WhenTestStage, ThenTestStage> {

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
public void failing_test_with_two_steps() {
given().an_exception_is_thrown();
when().something_happens();
}

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
public void failing_test_with_three_steps() {
given().an_exception_is_thrown();
Expand All @@ -25,20 +28,23 @@ public void failing_test_with_three_steps() {
}

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
public void failing_test_with_two_steps_and_second_step_fails() {
given().nothing();
when().a_step_fails();
}

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
public void failing_test_with_two_failing_stages() {
given().an_exception_is_thrown();
when().a_step_fails();
}

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
public void failing_test_where_second_stage_has_a_failing_after_stage_method() {
FailingAfterStageMethodStage stage = addStage( FailingAfterStageMethodStage.class );
Expand All @@ -47,6 +53,7 @@ public void failing_test_where_second_stage_has_a_failing_after_stage_method() {
}

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
@Pending
public void failing_test_with_Pending_annotation() {
Expand All @@ -55,34 +62,39 @@ public void failing_test_with_Pending_annotation() {
}

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
@Pending
public void passing_test_with_Pending_annotation() {
given().nothing();
}

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
@Pending( failIfPass = true )
public void passing_test_with_Pending_annotation_and_failIfPassed_set_to_true() {
given().nothing();
}

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
@Pending( failIfPass = true )
public void failing_test_with_Pending_annotation_and_failIfPassed_set_to_true() {
given().an_exception_is_thrown();
}

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
@Pending( failIfPass = true, executeSteps = true )
public void failing_test_with_Pending_annotation_and_executeSteps_set_to_true() {
given().an_exception_is_thrown();
}

@Test
@org.junit.jupiter.api.Test
@org.testng.annotations.Test
@TestTag( "testValue" )
public void test_with_tag_annotation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import org.junit.After;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.ExtendWith;

import com.tngtech.jgiven.junit.ScenarioTest;

public class TestWithExceptionsInAfterMethod extends ScenarioTestForTesting<GivenTestStage, WhenTestStage, ThenTestStage> {
@ExtendWith(JGivenReportExtractingExtension.class)
public class TestWithExceptionsInAfterMethod
extends ScenarioTestForTesting<GivenTestStage, WhenTestStage, ThenTestStage> {

@After
@AfterEach
public void afterException() {
throw new IllegalStateException( "exception in after method" );
throw new IllegalStateException("exception in after method");
}

@Test
@org.junit.jupiter.api.Test
public void test_that_exception_in_scenario_is_not_hidden_by_exception_in_JUnit_after_method() {
given().nothing();
when().a_step_fails();
Expand Down
Loading

0 comments on commit bc0a12b

Please sign in to comment.