Skip to content

Commit

Permalink
Implemented a more tidy fix for #3465
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed May 12, 2024
1 parent 765a9d0 commit 6c0e709
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 35 deletions.
7 changes: 0 additions & 7 deletions serenity-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,6 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>

<!-- QUESTIONABLE -->
<!-- <dependency>-->
<!-- <groupId>com.thoughtworks.xstream</groupId>-->
<!-- <artifactId>xstream</artifactId>-->
<!-- </dependency>-->

<!-- TEST DEPENDENCIES -->
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@
import net.thucydides.core.webdriver.ThucydidesWebDriverSupport;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.internal.AssumptionViolatedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -947,7 +945,12 @@ private Optional<TestOutcome> latestTestOutcome() {
}

private boolean isAssumptionFailure(Throwable rootCause) {
return (AssumptionViolatedException.class.isAssignableFrom(rootCause.getClass()));
try {
Class<?> assumptionViolationException = Class.forName("org.junit.AssumptionViolatedException");
return assumptionViolationException.isAssignableFrom(rootCause.getClass());
} catch (ClassNotFoundException var3) {
return false;
}
}

private String stepTitleFrom(io.cucumber.messages.types.Step currentStep, TestStep testStep) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.thucydides.model.steps.ExecutedStepDescription;
import net.thucydides.model.steps.StepFailure;
import net.thucydides.core.steps.events.StepEventBusEventBase;
import org.junit.internal.AssumptionViolatedException;

import java.time.ZonedDateTime;
import java.util.*;
Expand Down Expand Up @@ -119,7 +118,17 @@ private Optional<TestOutcome> latestTestOutcome() {
}

private boolean isAssumptionFailure(Throwable rootCause) {
return (AssumptionViolatedException.class.isAssignableFrom(rootCause.getClass()));
try {
// Load the AssumptionViolatedException class using its name
Class<?> clazz = Class.forName("org.junit.AssumptionViolatedException");

// Check if the rootCause is an instance of the AssumptionViolatedException class
return clazz.isAssignableFrom(rootCause.getClass());
} catch (ClassNotFoundException e) {
// AssumptionViolatedException class is not found on the classpath
return false;
}
//return (AssumptionViolatedException.class.isAssignableFrom(rootCause.getClass()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import net.thucydides.core.steps.BaseStepListener;
import net.thucydides.core.steps.StepEventBus;
import net.thucydides.core.steps.StepPublisher;
import org.junit.AssumptionViolatedException;
import org.junit.runners.model.Statement;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
* A JUnit statement that runs a Serenity-enabled test and then publishes the results via JUnit.
*/
Expand All @@ -31,17 +33,36 @@ public void evaluate() throws Throwable {
try {
updateCurrentEventBusFrom(publisher);
statement.evaluate();
} catch (AssumptionViolatedException assumptionViolated) {
stepEventBus().assumptionViolated(assumptionViolated.getMessage());
} catch (AssertionError assertionError) {
if (!stepEventBus().aStepInTheCurrentTestHasFailed()) {
throw assertionError;
} catch (Throwable throwable) {
// Attempt to check if the caught throwable is an instance of AssumptionViolatedException
if (isAssumptionViolatedException(throwable)) {
stepEventBus().assumptionViolated(throwable.getMessage());
} else if (throwable instanceof AssertionError) {
if (!stepEventBus().aStepInTheCurrentTestHasFailed()) {
throw throwable;
}
} else {
// Re-throw if it's neither an AssumptionViolatedException nor an AssertionError
throw throwable;
}
}

checkForStepFailures();
checkForAssumptionViolations();
}

private boolean isAssumptionViolatedException(Throwable throwable) {
try {
// Load the AssumptionViolatedException class using reflection
Class<?> clazz = Class.forName("org.junit.AssumptionViolatedException");
// Check if the throwable is an instance of AssumptionViolatedException
return clazz.isInstance(throwable);
} catch (ClassNotFoundException e) {
// Class not found, meaning it's not an AssumptionViolatedException
return false;
}
}

private void updateCurrentEventBusFrom(StepPublisher publisher) {
if (StepEventBus.getParallelEventBus() != stepEventBus()) {
StepEventBus.overrideEventBusWith(stepEventBus());
Expand All @@ -62,7 +83,27 @@ private void checkForStepFailures() throws Throwable {

private void checkForAssumptionViolations() {
if (stepEventBus().assumptionViolated()) {
throw new AssumptionViolatedException(stepEventBus().getAssumptionViolatedMessage());
// throw new AssumptionViolatedException(stepEventBus().getAssumptionViolatedMessage());
throw createAssumptionViolatedException(stepEventBus().getAssumptionViolatedMessage());
}
}

private RuntimeException createAssumptionViolatedException(String message) {
try {
// Load the AssumptionViolatedException class using reflection
Class<?> clazz = Class.forName("org.junit.AssumptionViolatedException");
// Get the constructor that takes a String as an argument
Constructor<?> constructor = clazz.getConstructor(String.class);
// Create a new instance of AssumptionViolatedException with the provided message
return (RuntimeException) constructor.newInstance(message);
} catch (ClassNotFoundException e) {
// Handle the case where the AssumptionViolatedException class is not found
System.err.println("AssumptionViolatedException class not found: " + e.getMessage());
return new RuntimeException("Assumption violation occurred, but no handler class found.");
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
// Handle other exceptions related to reflection and instantiation
System.err.println("Error instantiating AssumptionViolatedException: " + e.getMessage());
return new RuntimeException("Error during assumption violation exception handling.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean isSerenityTestCase(Class<?> testClass) {

@Override
public boolean isAssumptionViolatedException(final Throwable throwable) {
return (throwable instanceof org.junit.internal.AssumptionViolatedException);
return (throwable instanceof org.junit.AssumptionViolatedException);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ import spock.lang.Specification

import java.nio.file.Files

//import org.openqa.selenium.firefox.FirefoxDriver
//import org.openqa.selenium.htmlunit.HtmlUnitDriver

class WhenHandlingFailingTests extends Specification {

// def firefoxDriver = Mock(FirefoxDriver)
// def htmlUnitDriver = Mock(HtmlUnitDriver)
def environmentVariables = new MockEnvironmentVariables()
File temporaryDirectory

def setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.junit.internal.AssumptionViolatedException;

import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
Expand All @@ -21,12 +21,12 @@ public PassFailureCountingRule(int expectedPasses, int expectedFailures) {
this.expectedFailures = expectedFailures;
}

private class State {
private static class State {
private int passes = 0;
private int failures = 0;
}

private static final Map<Class<?>,State> states = new HashMap();
private static final Map<Class<?>,State> states = new HashMap<>();

@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
Expand All @@ -49,13 +49,14 @@ public void evaluate() throws Throwable {
base.evaluate();
result = "passed";
state.passes++;
} catch (AssumptionViolatedException e) {
result = "skipped";
} catch (Throwable t) {
result = "failed";
state.failures++;
cause = t;
}
if (isAssumptionViolatedException(t)) {
result = "skipped";
} else {
result = "failed";
state.failures++;
cause = t;
} }
if (state.passes > PassFailureCountingRule.this.expectedPasses) {
throw new AssertionError(this.createExceptionMessage(PassFailureCountingRule.this.expectedPasses,state.passes,"pass",testMethodName));
}
Expand All @@ -70,6 +71,15 @@ else if (state.failures > PassFailureCountingRule.this.expectedFailures) {
private String createExceptionMessage(int expected, int actual, String type, String testName) {
return "No more than " + expected + " test" + (expected==1?"":"s") + " in class " + testName + " should " + type + ", and this test " + type + "ing makes that " + actual + ".";
}

private boolean isAssumptionViolatedException(Throwable throwable) {
try {
Class<?> clazz = Class.forName("org.junit.AssumptionViolatedException");
return clazz.isInstance(throwable);
} catch (ClassNotFoundException e) {
return false;
}
}
};
}
}

0 comments on commit 6c0e709

Please sign in to comment.