Skip to content

Commit

Permalink
[hotfix][test-utils-junit] Improve exception assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Apr 1, 2022
1 parent 39854d1 commit f9f4284
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
import java.util.function.Consumer;
import java.util.regex.Pattern;

import static org.apache.flink.core.testutils.FlinkAssertions.containsCause;
import static org.apache.flink.core.testutils.FlinkAssertions.anyCauseMatches;
import static org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptionsUtil.AVRO_CONFLUENT;
import static org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptionsUtil.DEBEZIUM_AVRO_CONFLUENT;
import static org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptionsUtil.PROPERTIES_PREFIX;
Expand Down Expand Up @@ -744,9 +744,9 @@ public void testSourceTableWithTopicAndTopicPattern() {
})
.isInstanceOf(ValidationException.class)
.satisfies(
containsCause(
new ValidationException(
"Option 'topic' and 'topic-pattern' shouldn't be set together.")));
anyCauseMatches(
ValidationException.class,
"Option 'topic' and 'topic-pattern' shouldn't be set together."));
}

@Test
Expand All @@ -763,10 +763,10 @@ public void testMissingStartupTimestamp() {
})
.isInstanceOf(ValidationException.class)
.satisfies(
containsCause(
new ValidationException(
"'scan.startup.timestamp-millis' "
+ "is required in 'timestamp' startup mode but missing.")));
anyCauseMatches(
ValidationException.class,
"'scan.startup.timestamp-millis' "
+ "is required in 'timestamp' startup mode but missing."));
}

@Test
Expand All @@ -784,10 +784,10 @@ public void testMissingSpecificOffsets() {
})
.isInstanceOf(ValidationException.class)
.satisfies(
containsCause(
new ValidationException(
"'scan.startup.specific-offsets' "
+ "is required in 'specific-offsets' startup mode but missing.")));
anyCauseMatches(
ValidationException.class,
"'scan.startup.specific-offsets' "
+ "is required in 'specific-offsets' startup mode but missing."));
}

@Test
Expand All @@ -803,10 +803,9 @@ public void testInvalidSinkPartitioner() {
})
.isInstanceOf(ValidationException.class)
.satisfies(
containsCause(
new ValidationException(
"Could not find and instantiate partitioner "
+ "class 'abc'")));
anyCauseMatches(
ValidationException.class,
"Could not find and instantiate partitioner " + "class 'abc'"));
}

@Test
Expand All @@ -823,10 +822,10 @@ public void testInvalidRoundRobinPartitionerWithKeyFields() {
})
.isInstanceOf(ValidationException.class)
.satisfies(
containsCause(
new ValidationException(
"Currently 'round-robin' partitioner only works "
+ "when option 'key.fields' is not specified.")));
anyCauseMatches(
ValidationException.class,
"Currently 'round-robin' partitioner only works "
+ "when option 'key.fields' is not specified."));
}

@Test
Expand All @@ -850,9 +849,9 @@ public void testExactlyOnceGuaranteeWithoutTransactionalIdPrefix() {
})
.isInstanceOf(ValidationException.class)
.satisfies(
containsCause(
new ValidationException(
"sink.transactional-id-prefix must be specified when using DeliveryGuarantee.EXACTLY_ONCE.")));
anyCauseMatches(
ValidationException.class,
"sink.transactional-id-prefix must be specified when using DeliveryGuarantee.EXACTLY_ONCE."));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,9 @@ void testOnStartFails() throws Exception {
onStartEndpoint.awaitUntilOnStartCalled();

assertThatThrownBy(() -> onStartEndpoint.getTerminationFuture().get())
.satisfies(FlinkAssertions.containsCause(testException));
.satisfies(
FlinkAssertions.anyCauseMatches(
testException.getClass(), testException.getMessage()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,16 @@ public static ThrowingConsumer<? super Throwable> anyCauseMatches(
Class<? extends Throwable> clazz, String containsMessage) {
return t ->
assertThatChainOfCauses(t)
.as(
"Any cause is instance of class '%s' and contains message '%s'",
clazz, containsMessage)
.anySatisfy(
cause ->
assertThat(cause)
.isInstanceOf(clazz)
.hasMessageContaining(containsMessage));
}

/**
* Shorthand to assert the chain of causes includes a specific {@link Throwable}. Same as:
*
* <pre>{@code
* assertThatChainOfCauses(t)
* .anySatisfy(
* cause ->
* assertThat(cause)
* .isInstanceOf(throwable.getClass())
* .hasMessage(throwable.getMessage()));
* }</pre>
*/
public static ThrowingConsumer<? super Throwable> containsCause(Throwable throwable) {
return t ->
assertThatChainOfCauses(t)
.anySatisfy(
cause ->
assertThat(cause)
.isInstanceOf(throwable.getClass())
.hasMessage(throwable.getMessage()));
}

/**
* Shorthand to assert the chain of causes includes a {@link Throwable} matching a specific
* {@link Class}. Same as:
Expand All @@ -102,6 +83,7 @@ public static ThrowingConsumer<? super Throwable> anyCauseMatches(
Class<? extends Throwable> clazz) {
return t ->
assertThatChainOfCauses(t)
.as("Any cause is instance of class '%s'", clazz)
.anySatisfy(cause -> assertThat(cause).isInstanceOf(clazz));
}

Expand All @@ -120,6 +102,7 @@ public static ThrowingConsumer<? super Throwable> anyCauseMatches(
public static ThrowingConsumer<? super Throwable> anyCauseMatches(String containsMessage) {
return t ->
assertThatChainOfCauses(t)
.as("Any cause contains message '%s'", containsMessage)
.anySatisfy(t1 -> assertThat(t1).hasMessageContaining(containsMessage));
}

Expand Down

0 comments on commit f9f4284

Please sign in to comment.