From a8a259a5574045bbb38061857c3d64baf123b6eb Mon Sep 17 00:00:00 2001 From: l-1squared <30831153+l-1squared@users.noreply.github.com> Date: Mon, 17 Jun 2024 15:57:37 +0200 Subject: [PATCH] feat(Issue-1625): Introduce new ABORTED status adapt unit tests Signed-off-by: l-1squared <30831153+l-1squared@users.noreply.github.com> --- .../jgiven/impl/ScenarioModelBuilder.java | 4 +-- .../jgiven/report/model/ExecutionStatus.java | 3 +- .../jgiven/report/model/StepStatus.java | 3 +- .../jgiven/junit/JGivenMethodRule.java | 5 ++- .../tngtech/jgiven/junit/AssumptionTest.java | 33 ++++++++----------- .../jgiven/junit5/JGivenExtension.java | 7 ++-- .../jgiven/junit5/test/AssumptionsTest.java | 29 +++++++--------- .../jgiven/testng/AssumptionsTest.java | 4 +-- 8 files changed, 39 insertions(+), 49 deletions(-) diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioModelBuilder.java b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioModelBuilder.java index 6044c91a913..528f334f5ab 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioModelBuilder.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioModelBuilder.java @@ -321,7 +321,7 @@ public void stepMethodFailed(Throwable t) { @Override public void stepMethodAborted(Throwable t) { if (currentStep != null) { - currentStep.setStatus(StepStatus.SKIPPED); + currentStep.setStatus(StepStatus.ABORTED); } } @@ -377,7 +377,7 @@ public void scenarioFailed(Throwable e) { @Override public void scenarioAborted(Throwable e){ - setStatus(ExecutionStatus.SOME_STEPS_PENDING); + setStatus(ExecutionStatus.ABORTED); setException(e); } diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ExecutionStatus.java b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ExecutionStatus.java index 98def5817a1..c1054ee4422 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ExecutionStatus.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ExecutionStatus.java @@ -4,5 +4,6 @@ public enum ExecutionStatus { SCENARIO_PENDING, SUCCESS, FAILED, - SOME_STEPS_PENDING; + ABORTED, + SOME_STEPS_PENDING } \ No newline at end of file diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/StepStatus.java b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/StepStatus.java index d9d7d6808ae..698a7477d3e 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/StepStatus.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/StepStatus.java @@ -4,5 +4,6 @@ public enum StepStatus { PASSED, FAILED, SKIPPED, - PENDING; + PENDING, + ABORTED } diff --git a/jgiven-junit/src/main/java/com/tngtech/jgiven/junit/JGivenMethodRule.java b/jgiven-junit/src/main/java/com/tngtech/jgiven/junit/JGivenMethodRule.java index 50369e6ac9c..b70e44f1312 100644 --- a/jgiven-junit/src/main/java/com/tngtech/jgiven/junit/JGivenMethodRule.java +++ b/jgiven-junit/src/main/java/com/tngtech/jgiven/junit/JGivenMethodRule.java @@ -23,8 +23,7 @@ import java.lang.reflect.Field; import java.util.*; -import static com.tngtech.jgiven.report.model.ExecutionStatus.FAILED; -import static com.tngtech.jgiven.report.model.ExecutionStatus.SUCCESS; +import static com.tngtech.jgiven.report.model.ExecutionStatus.*; import static java.lang.String.format; import static org.junit.Assume.assumeTrue; @@ -87,7 +86,7 @@ protected void succeeded() throws Throwable { scenario.finished(); // ignore test when scenario is not implemented - assumeTrue( EnumSet.of( SUCCESS, FAILED ).contains( scenario.getScenarioModel().getExecutionStatus() ) ); + assumeTrue( EnumSet.of( SUCCESS, FAILED, ABORTED).contains( scenario.getScenarioModel().getExecutionStatus() ) ); } protected void failed( Throwable e ) throws Throwable { diff --git a/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/AssumptionTest.java b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/AssumptionTest.java index df506bfbab2..91248797d06 100644 --- a/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/AssumptionTest.java +++ b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/AssumptionTest.java @@ -4,32 +4,36 @@ import com.tngtech.jgiven.report.model.ScenarioCaseModel; import com.tngtech.jgiven.report.model.StepStatus; import org.junit.Assume; +import org.junit.AssumptionViolatedException; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assumptions.assumeThat; -@Description("Scenarios can have sections") +@Description("Scenarios can have assumptions") public class AssumptionTest extends SimpleScenarioTest { @Test - public void should_pass_on_assertJ_assumptions() throws Throwable { - assertThatThrownBy(() -> when().I_assume_something_using_assertJ()) - .isInstanceOf(catchException(AssumptionTest::assertJAssumptionFailure)); + public void should_report_aborted_on_assertJ_assumptions() throws Throwable { + when().I_assume_something_using_assertJ(); getScenario().finished(); + + assertThat(getScenario().getExecutor().hasAborted()).isTrue(); + assertThat(getScenario().getExecutor().getAbortedException()).isInstanceOf(AssumptionViolatedException.class); ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase(0); - assertThat(aCase.getStep(0).getStatus()).isEqualTo(StepStatus.PASSED); + assertThat(aCase.getStep(0).getStatus()).isEqualTo(StepStatus.ABORTED); } @Test - public void should_pass_on_junit5_assumptions() throws Throwable { - assertThatThrownBy(() -> when().I_assume_something_using_junit5()) - .isInstanceOf(catchException(AssumptionTest::junitAssumptionFailure)); + public void should_report_aborted_on_junit_assumptions() throws Throwable { + when().I_assume_something_using_junit5(); getScenario().finished(); + + assertThat(getScenario().getExecutor().hasAborted()).isTrue(); + assertThat(getScenario().getExecutor().getAbortedException()).isInstanceOf(org.junit.AssumptionViolatedException.class); ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase(0); - assertThat(aCase.getStep(0).getStatus()).isEqualTo(StepStatus.PASSED); + assertThat(aCase.getStep(0).getStatus()).isEqualTo(StepStatus.ABORTED); } static class TestStage { @@ -50,13 +54,4 @@ private static void assertJAssumptionFailure() { private static void junitAssumptionFailure() { Assume.assumeTrue(false); } - - private Class catchException(Runnable runnable) { - try { - runnable.run(); - return null; - } catch (Exception e) { - return e.getClass(); - } - } } \ No newline at end of file diff --git a/jgiven-junit5/src/main/java/com/tngtech/jgiven/junit5/JGivenExtension.java b/jgiven-junit5/src/main/java/com/tngtech/jgiven/junit5/JGivenExtension.java index 7fcfb9cff62..f0ad18aa773 100644 --- a/jgiven-junit5/src/main/java/com/tngtech/jgiven/junit5/JGivenExtension.java +++ b/jgiven-junit5/src/main/java/com/tngtech/jgiven/junit5/JGivenExtension.java @@ -1,8 +1,5 @@ package com.tngtech.jgiven.junit5; -import static com.tngtech.jgiven.report.model.ExecutionStatus.FAILED; -import static com.tngtech.jgiven.report.model.ExecutionStatus.SUCCESS; - import com.tngtech.jgiven.base.ScenarioTestBase; import com.tngtech.jgiven.config.AbstractJGivenConfiguration; import com.tngtech.jgiven.config.ConfigurationUtil; @@ -24,6 +21,8 @@ import org.junit.jupiter.api.extension.ExtensionContext.Namespace; import org.junit.jupiter.api.extension.TestInstancePostProcessor; +import static com.tngtech.jgiven.report.model.ExecutionStatus.*; + /** * This extension enables JGiven for JUnit 5 Tests. *

@@ -93,7 +92,7 @@ public void afterTestExecution(ExtensionContext context) throws Exception { // ignore test when scenario is not implemented Assumptions.assumeTrue( - EnumSet.of(SUCCESS, FAILED).contains(scenario.getScenarioModel().getExecutionStatus())); + EnumSet.of(SUCCESS, FAILED, ABORTED).contains(scenario.getScenarioModel().getExecutionStatus())); } catch (Exception e) { throw e; diff --git a/jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/AssumptionsTest.java b/jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/AssumptionsTest.java index d50904a23ab..ffbec84bc68 100644 --- a/jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/AssumptionsTest.java +++ b/jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/AssumptionsTest.java @@ -7,7 +7,6 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assumptions.assumeThat; class AssumptionsTest extends SimpleScenarioTest { @@ -15,20 +14,24 @@ class AssumptionsTest extends SimpleScenarioTest { @Test void should_pass_on_assertJ_assumptions() throws Throwable { - assertThatThrownBy(() -> when().I_assume_something_using_assertJ()) - .isInstanceOf(catchException(AssumptionsTest::assertJAssumptionFailure)); + when().I_assume_something_using_assertJ(); getScenario().finished(); - ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 ); - assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED ); + + assertThat(getScenario().getExecutor().hasAborted()).isTrue(); + assertThat(getScenario().getExecutor().getAbortedException()).isInstanceOf(org.junit.AssumptionViolatedException.class); + ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase(0); + assertThat(aCase.getStep(0).getStatus()).isEqualTo(StepStatus.ABORTED); } @Test void should_pass_on_junit5_assumptions() throws Throwable { - assertThatThrownBy(() -> when().I_assume_something_using_junit5()) - .isInstanceOf(catchException(AssumptionsTest::junitAssumptionFailure)); + when().I_assume_something_using_junit5(); getScenario().finished(); - ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 ); - assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED ); + + assertThat(getScenario().getExecutor().hasAborted()).isTrue(); + assertThat(getScenario().getExecutor().getAbortedException()).isInstanceOf(org.opentest4j.TestAbortedException.class); + ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase(0); + assertThat(aCase.getStep(0).getStatus()).isEqualTo(StepStatus.ABORTED); } static class TestStage { @@ -48,12 +51,4 @@ private static void assertJAssumptionFailure(){ private static void junitAssumptionFailure(){ Assumptions.abort(); } - private Class catchException(Runnable runnable) { - try { - runnable.run(); - return null; - } catch (Exception e) { - return e.getClass(); - } - } } diff --git a/jgiven-testng/src/test/java/com/tngtech/jgiven/testng/AssumptionsTest.java b/jgiven-testng/src/test/java/com/tngtech/jgiven/testng/AssumptionsTest.java index bb7c5a8ffce..0646f2c8b41 100644 --- a/jgiven-testng/src/test/java/com/tngtech/jgiven/testng/AssumptionsTest.java +++ b/jgiven-testng/src/test/java/com/tngtech/jgiven/testng/AssumptionsTest.java @@ -19,7 +19,7 @@ public void should_pass_on_assertJ_assumptions() throws Throwable { .isInstanceOf(catchException(AssumptionsTest::assertJAssumptionFailure)); getScenario().finished(); ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 ); - assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED ); + assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.ABORTED); } @Test @@ -28,7 +28,7 @@ public void should_pass_on_testng_assumptions() throws Throwable { .isInstanceOf(catchException(AssumptionsTest::testNgAssumptionFailure)); getScenario().finished(); ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 ); - assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED ); + assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.ABORTED); } static class TestStage {