Skip to content

Commit

Permalink
Issue-1465: Make JGiven pass on assertions from JUnit5
Browse files Browse the repository at this point in the history
and also test that passing on assumption failures works for all testframeworks

Signed-off-by: l-1squared <30831153+l-1squared@users.noreply.github.com>
  • Loading branch information
l-1squared committed Dec 8, 2023
1 parent 2817a83 commit b0c5815
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Release v1.3.1
## Fixed issues
* TextReportGenerator is now backed by jansi2 not jansi1. [#1420](https://github.com/TNG/JGiven/issues/1420)
* JUnit 5 assumptions failures don't fail tests anymore [#1465] (https://github.com/TNG/JGiven/issues/1465) (thanks to j8zdev for the analysis)
* Further dependency updates.

# Release v1.3.0
## Breaking changes
* Increased minimum supported version to Java 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

public class ThrowableUtil {
public static boolean isAssumptionException(Throwable t) {
return t.getClass().getName().equals( "org.junit.AssumptionViolatedException" )
|| t.getClass().getName().equals( "org.testng.SkipException");
return t.getClass().getName().equals("org.junit.AssumptionViolatedException")
|| t.getClass().getName().equals("org.opentest4j.TestAbortedException")
|| t.getClass().getName().equals("org.testng.SkipException");
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
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;
import org.junit.Assume;
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")
public class AssumptionTest extends SimpleScenarioTest<AssumptionTest.TestStage> {

@Description( "Scenarios can have sections" )
public class AssumptionTest extends ScenarioTest<GivenTestStep, WhenTestStep, ThenTestStep> {

@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 ) {}
public void should_pass_on_assertJ_assumptions() throws Throwable {
assertThatThrownBy(() -> when().I_assume_something_using_assertJ())
.isInstanceOf(catchException(AssumptionTest::assertJAssumptionFailure));
getScenario().finished();
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase(0);
assertThat(aCase.getStep(0).getStatus()).isEqualTo(StepStatus.PASSED);
}

@Test
public void should_pass_on_junit5_assumptions() throws Throwable {
assertThatThrownBy(() -> when().I_assume_something_using_junit5())
.isInstanceOf(catchException(AssumptionTest::junitAssumptionFailure));
getScenario().finished();
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase(0);
assertThat(aCase.getStep(0).getStatus()).isEqualTo(StepStatus.PASSED);
}

static class TestStage {
void I_assume_something_using_assertJ() {
assertJAssumptionFailure();
}

void I_assume_something_using_junit5() {
junitAssumptionFailure();
}
}

private static void assertJAssumptionFailure() {
assumeThat(true).isFalse();
}

ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 );
assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED );
@SuppressWarnings("DataFlowIssue")//we want to provoke an assumption failure
private static void junitAssumptionFailure() {
Assume.assumeTrue(false);
}

}
private Class<? extends Exception> catchException(Runnable runnable) {
try {
runnable.run();
return null;
} catch (Exception e) {
return e.getClass();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.tngtech.jgiven.junit.test;

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;
Expand All @@ -28,7 +24,4 @@ public void multiply_with_two() {

public void something() {}

public void some_assumption_fails() {
Assume.assumeTrue( false );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.tngtech.jgiven.junit5.test;

import com.tngtech.jgiven.junit5.SimpleScenarioTest;
import com.tngtech.jgiven.report.model.ScenarioCaseModel;
import com.tngtech.jgiven.report.model.StepStatus;
import org.junit.jupiter.api.Assumptions;
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<AssumptionsTest.TestStage> {


@Test
void should_pass_on_assertJ_assumptions() throws Throwable {
assertThatThrownBy(() -> when().I_assume_something_using_assertJ())
.isInstanceOf(catchException(AssumptionsTest::assertJAssumptionFailure));
getScenario().finished();
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 );
assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED );
}

@Test
void should_pass_on_junit5_assumptions() throws Throwable {
assertThatThrownBy(() -> when().I_assume_something_using_junit5())
.isInstanceOf(catchException(AssumptionsTest::junitAssumptionFailure));
getScenario().finished();
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 );
assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED );
}

static class TestStage {
void I_assume_something_using_assertJ() {
assertJAssumptionFailure();
}

void I_assume_something_using_junit5() {
junitAssumptionFailure();
}
}

private static void assertJAssumptionFailure(){
assumeThat( true ).isFalse();
}

private static void junitAssumptionFailure(){
Assumptions.abort();
}
private Class<? extends Exception> catchException(Runnable runnable) {
try {
runnable.run();
return null;
} catch (Exception e) {
return e.getClass();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public void JGiven_works_with_JUnit5_DISABLED() {
then().some_outcome();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.tngtech.jgiven.testng;

import com.tngtech.jgiven.report.model.ScenarioCaseModel;
import com.tngtech.jgiven.report.model.StepStatus;
import org.testng.SkipException;
import org.testng.annotations.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;

@Test(singleThreaded = true)
public class AssumptionsTest extends SimpleScenarioTest<AssumptionsTest.TestStage> {


@Test
public void should_pass_on_assertJ_assumptions() throws Throwable {
assertThatThrownBy(() -> when().I_assume_something_using_assertJ())
.isInstanceOf(catchException(AssumptionsTest::assertJAssumptionFailure));
getScenario().finished();
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 );
assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED );
}

@Test
public void should_pass_on_junit5_assumptions() throws Throwable {
assertThatThrownBy(() -> when().I_assume_something_using_junit5())
.isInstanceOf(catchException(AssumptionsTest::testNgAssumptionFailure));
getScenario().finished();
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 );
assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED );
}

static class TestStage {
void I_assume_something_using_assertJ() {
assertJAssumptionFailure();
}

void I_assume_something_using_junit5() {
testNgAssumptionFailure();
}
}

private static void assertJAssumptionFailure(){
assumeThat( true ).isFalse();
}

private static void testNgAssumptionFailure(){
throw new SkipException("TestNG assumption failure");
}
private Class<? extends Exception> catchException(Runnable runnable) {
try {
runnable.run();
return null;
} catch (Exception e) {
return e.getClass();
}
}
}

0 comments on commit b0c5815

Please sign in to comment.