Skip to content

Commit

Permalink
Change precedence of assumption classes (#1983)
Browse files Browse the repository at this point in the history
In order of priority:
* org.opentest4j.TestAbortedException
* org.testng.SkipException
* org.junit.AssumptionViolatedException
  • Loading branch information
scordio authored Aug 28, 2020
1 parent 8a9712c commit 7279cd7
Show file tree
Hide file tree
Showing 20 changed files with 516 additions and 525 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/assertj/core/api/Assumptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -1293,16 +1293,16 @@ protected static <ASSERTION> Class<? extends ASSERTION> generateAssumptionClass(
}

private static RuntimeException assumptionNotMet(AssertionError assertionError) throws ReflectiveOperationException {
Class<?> assumptionClass = getAssumptionClass("org.junit.AssumptionViolatedException");
Class<?> assumptionClass = getAssumptionClass("org.opentest4j.TestAbortedException");
if (assumptionClass != null) return assumptionNotMet(assumptionClass, assertionError);

assumptionClass = getAssumptionClass("org.opentest4j.TestAbortedException");
assumptionClass = getAssumptionClass("org.testng.SkipException");
if (assumptionClass != null) return assumptionNotMet(assumptionClass, assertionError);

assumptionClass = getAssumptionClass("org.testng.SkipException");
assumptionClass = getAssumptionClass("org.junit.AssumptionViolatedException");
if (assumptionClass != null) return assumptionNotMet(assumptionClass, assertionError);

throw new IllegalStateException("Assumptions require JUnit, opentest4j or TestNG on the classpath");
throw new IllegalStateException("Assumptions require opentest4j, TestNG or JUnit on the classpath");
}

private static Class<?> getAssumptionClass(String className) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
package org.assertj.core.api.assumptions;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.assertj.core.util.Arrays.array;
import static org.assertj.core.util.AssertionsUtil.expectAssumptionNotMetException;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -31,7 +31,6 @@
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.stream.Stream;

import org.junit.AssumptionViolatedException;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand All @@ -41,154 +40,154 @@ class Assumptions_assumeThat_Atomics_Test {

static Stream<AssumptionRunner<?>> provideAssumptionsRunners() {
return Stream.of(
new AssumptionRunner<AtomicBoolean>(new AtomicBoolean(true)) {
@Override
public void runFailingAssumption() {
assumeThat(actual).isFalse();
}

@Override
public void runPassingAssumption() {
assumeThat(actual).isTrue();
}
},
new AssumptionRunner<AtomicInteger>(new AtomicInteger(42)) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasNegativeValue();
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasPositiveValue();
}
},
new AssumptionRunner<AtomicIntegerArray>(new AtomicIntegerArray(new int[] { 2, 5, 7 })) {
@Override
public void runFailingAssumption() {
assumeThat(actual).contains(20);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).contains(7);
}
},
new AssumptionRunner<AtomicIntegerFieldUpdater<VolatileFieldsHolder>>(AtomicIntegerFieldUpdater.newUpdater(VolatileFieldsHolder.class,
"intValue")) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasValue(10, VOLATILE_FIELDS_HOLDER);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasValue(0, VOLATILE_FIELDS_HOLDER);
}
},
new AssumptionRunner<AtomicLong>(new AtomicLong(42)) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasNegativeValue();
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasPositiveValue();
}
},
new AssumptionRunner<AtomicLongArray>(new AtomicLongArray(new long[] { 2, 5, 7 })) {
@Override
public void runFailingAssumption() {
assumeThat(actual).contains(20);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).contains(7);
}
},
new AssumptionRunner<AtomicLongFieldUpdater<VolatileFieldsHolder>>(AtomicLongFieldUpdater.newUpdater(VolatileFieldsHolder.class,
"longValue")) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasValue(10L, VOLATILE_FIELDS_HOLDER);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasValue(0L, VOLATILE_FIELDS_HOLDER);
}
},
new AssumptionRunner<AtomicReference<String>>(new AtomicReference<>("test")) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasValue("other");
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasValue("test");
}
},
new AssumptionRunner<AtomicReferenceArray<String>>(new AtomicReferenceArray<>(array("2", "5", "7"))) {
@Override
public void runFailingAssumption() {
assumeThat(actual).contains("20");
}

@Override
public void runPassingAssumption() {
assumeThat(actual).contains("7");
}
},
new AssumptionRunner<AtomicReferenceFieldUpdater<VolatileFieldsHolder, String>>(AtomicReferenceFieldUpdater.newUpdater(VolatileFieldsHolder.class,
String.class,
"stringValue")) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasValue("other", VOLATILE_FIELDS_HOLDER);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasValue("test", VOLATILE_FIELDS_HOLDER);
}
},
new AssumptionRunner<AtomicMarkableReference<String>>(new AtomicMarkableReference<>("test", true)) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasReference("other");
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasReference("test");
}
},
new AssumptionRunner<AtomicStampedReference<String>>(new AtomicStampedReference<>("test", 1)) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasStamp(0);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasStamp(1);
}
});
new AssumptionRunner<AtomicBoolean>(new AtomicBoolean(true)) {
@Override
public void runFailingAssumption() {
assumeThat(actual).isFalse();
}

@Override
public void runPassingAssumption() {
assumeThat(actual).isTrue();
}
},
new AssumptionRunner<AtomicInteger>(new AtomicInteger(42)) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasNegativeValue();
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasPositiveValue();
}
},
new AssumptionRunner<AtomicIntegerArray>(new AtomicIntegerArray(new int[] { 2, 5, 7 })) {
@Override
public void runFailingAssumption() {
assumeThat(actual).contains(20);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).contains(7);
}
},
new AssumptionRunner<AtomicIntegerFieldUpdater<VolatileFieldsHolder>>(AtomicIntegerFieldUpdater.newUpdater(VolatileFieldsHolder.class,
"intValue")) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasValue(10, VOLATILE_FIELDS_HOLDER);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasValue(0, VOLATILE_FIELDS_HOLDER);
}
},
new AssumptionRunner<AtomicLong>(new AtomicLong(42)) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasNegativeValue();
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasPositiveValue();
}
},
new AssumptionRunner<AtomicLongArray>(new AtomicLongArray(new long[] { 2, 5, 7 })) {
@Override
public void runFailingAssumption() {
assumeThat(actual).contains(20);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).contains(7);
}
},
new AssumptionRunner<AtomicLongFieldUpdater<VolatileFieldsHolder>>(AtomicLongFieldUpdater.newUpdater(VolatileFieldsHolder.class,
"longValue")) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasValue(10L, VOLATILE_FIELDS_HOLDER);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasValue(0L, VOLATILE_FIELDS_HOLDER);
}
},
new AssumptionRunner<AtomicReference<String>>(new AtomicReference<>("test")) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasValue("other");
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasValue("test");
}
},
new AssumptionRunner<AtomicReferenceArray<String>>(new AtomicReferenceArray<>(array("2", "5", "7"))) {
@Override
public void runFailingAssumption() {
assumeThat(actual).contains("20");
}

@Override
public void runPassingAssumption() {
assumeThat(actual).contains("7");
}
},
new AssumptionRunner<AtomicReferenceFieldUpdater<VolatileFieldsHolder, String>>(AtomicReferenceFieldUpdater.newUpdater(VolatileFieldsHolder.class,
String.class,
"stringValue")) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasValue("other", VOLATILE_FIELDS_HOLDER);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasValue("test", VOLATILE_FIELDS_HOLDER);
}
},
new AssumptionRunner<AtomicMarkableReference<String>>(new AtomicMarkableReference<>("test", true)) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasReference("other");
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasReference("test");
}
},
new AssumptionRunner<AtomicStampedReference<String>>(new AtomicStampedReference<>("test", 1)) {
@Override
public void runFailingAssumption() {
assumeThat(actual).hasStamp(0);
}

@Override
public void runPassingAssumption() {
assumeThat(actual).hasStamp(1);
}
});
}

@ParameterizedTest
@MethodSource("provideAssumptionsRunners")
void should_ignore_test_when_assumption_fails(AssumptionRunner<?> assumptionRunner) {
assertThatExceptionOfType(AssumptionViolatedException.class).isThrownBy(() -> assumptionRunner.runFailingAssumption());
expectAssumptionNotMetException(assumptionRunner::runFailingAssumption);
}

@ParameterizedTest
@MethodSource("provideAssumptionsRunners")
void should_run_test_when_assumption_passes(AssumptionRunner<?> assumptionRunner) {
assertThatCode(() -> assumptionRunner.runPassingAssumption()).doesNotThrowAnyException();
assertThatCode(assumptionRunner::runPassingAssumption).doesNotThrowAnyException();
}

@SuppressWarnings("unused")
Expand Down
Loading

0 comments on commit 7279cd7

Please sign in to comment.