Skip to content

Commit

Permalink
feature: configuration key to break test if events consumer fail
Browse files Browse the repository at this point in the history
  • Loading branch information
giulong committed Dec 26, 2024
1 parent 8ae0dd5 commit 487e352
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion it-testbook/src/test/resources/configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ testBook:
total: 3
successful: 2

# these will fail since the slack consumer is lacking the token. We use them to check the log file and verify they're consumed
# these will fail since custom events lack the extension context used in the default slack.json template. We use them to check the log file and verify they're consumed
eventsConsumers:
- slack:
events:
Expand Down
2 changes: 1 addition & 1 deletion it/src/test/resources/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extent:
name: { }

eventsConsumers:
- slack: # these will fail since the slack consumer is lacking the token. We use them to check the log file and verify they're consumed
- slack: # these will fail since custom events lack the extension context used in the default slack.json template. We use them to check the log file and verify they're consumed
events:
- primaryId: primary.* # custom event by primaryId regex
reason: custom-event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public abstract class EventsConsumer implements Consumer<Event> {
@JsonPropertyDescription("List of events that will be consumed")
protected List<Event> events;

@SuppressWarnings("unused")
@JsonPropertyDescription("Set to true to fail the test on consumer's exceptions")
private boolean failOnError;

public void match(final Event event) {
events
.stream()
Expand Down Expand Up @@ -96,6 +100,10 @@ void acceptSilently(final Event event) {
accept(event);
} catch (Exception e) {
log.error(String.format("%s: %s", getClass().getSimpleName(), e.getMessage()), e);

if (failOnError) {
throw e;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.giulong.spectrum.enums.Result;
import io.github.giulong.spectrum.pojos.events.Event;
import io.github.giulong.spectrum.utils.Reflections;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -194,6 +195,22 @@ void acceptSilently() {
assertDoesNotThrow(() -> eventsConsumer.acceptSilently(event), exceptionMessage);
}

@Test
@DisplayName("acceptSilently should rethrow the exception if failOnError is true")
void acceptSilentlyThrow() {
final String exceptionMessage = "THE STACKTRACE BELOW IS EXPECTED!!!";
final Event event = mock(Event.class);

Reflections.setField("failOnError", eventsConsumer, true);

when(event.getContext()).thenThrow(new RuntimeException(exceptionMessage));

eventsConsumer.events = List.of(event);
final Exception exception = assertThrows(RuntimeException.class, () -> eventsConsumer.acceptSilently(event));

assertEquals(exceptionMessage, exception.getMessage());
}

private static class DummyEventsConsumer extends EventsConsumer {

DummyEventsConsumer() {
Expand Down

0 comments on commit 487e352

Please sign in to comment.