diff --git a/CHANGELOG.md b/CHANGELOG.md index 57f0b50de0..16e86e223a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ## Fixed Issues * HTML Report: fixed an issue with the search input in the mobile menu that was hidden on mobile devices when the virtual keyboard appeared. [#182](https://github.com/TNG/JGiven/issues/182) +* JUnit: throwing an AssumptionViolationException will not lead to a failed scenario anymore. Instead, the scenario will be ignored and will not appear in the report at all. [#185](https://github.com/TNG/JGiven/issues/185) # v0.11.0 diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/StandaloneScenarioExecutor.java b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/StandaloneScenarioExecutor.java index 239cad09d7..bf9062657f 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/StandaloneScenarioExecutor.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/StandaloneScenarioExecutor.java @@ -129,6 +129,10 @@ public void handleMethod( Object stageInstance, Method paramMethod, Object[] arg @Override public void handleThrowable( Throwable t ) throws Throwable { + if( t.getClass().getName().equals( "org.junit.AssumptionViolatedException" ) ) { + throw t; + } + listener.stepMethodFailed( t ); failed( t ); } 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 new file mode 100644 index 0000000000..73a30cd948 --- /dev/null +++ b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/AssumptionTest.java @@ -0,0 +1,32 @@ +package com.tngtech.jgiven.junit; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.assertj.core.api.Assertions; +import org.junit.AssumptionViolatedException; +import org.junit.Test; + +import com.tngtech.jgiven.annotation.Description; +import com.tngtech.jgiven.junit.test.GivenTestStep; +import com.tngtech.jgiven.junit.test.ThenTestStep; +import com.tngtech.jgiven.junit.test.WhenTestStep; +import com.tngtech.jgiven.report.model.ScenarioCaseModel; +import com.tngtech.jgiven.report.model.StepStatus; + +@Description( "Scenarios can have sections" ) +public class AssumptionTest extends ScenarioTest { + + @Test + public void JUnit_assumption_exceptions_should_be_treated_correctly() throws Throwable { + try { + when().some_assumption_fails(); + Assertions.fail( "AssumptionViolationException should have been thrown" ); + } catch( AssumptionViolatedException e ) {} + + getScenario().finished(); + + ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 ); + assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED ); + } + +} diff --git a/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/test/WhenTestStep.java b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/test/WhenTestStep.java index 4f34ea5370..abb4cfe432 100644 --- a/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/test/WhenTestStep.java +++ b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/test/WhenTestStep.java @@ -2,6 +2,8 @@ import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Assume; + import com.tngtech.jgiven.Stage; import com.tngtech.jgiven.annotation.ExpectedScenarioState; import com.tngtech.jgiven.annotation.ProvidedScenarioState; @@ -29,4 +31,8 @@ public void something() {} public void some_assertion_fails() { assertThat( true ).isFalse(); } + + public void some_assumption_fails() { + Assume.assumeTrue( false ); + } }