Skip to content

Commit

Permalink
Action: Improve catching and rethrowing of RuntimException
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaldvogel committed Sep 24, 2024
1 parent 5037186 commit de7264b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/de/cronn/commons/lang/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ default Supplier<Void> toSupplier() {
return () -> {
try {
execute();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/de/cronn/commons/lang/ActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.*;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

import org.junit.jupiter.api.Test;

Expand All @@ -20,6 +21,20 @@ void testToSupplier() {
assertThat(numberOfExecutions.get()).isEqualTo(1);
}

@Test
void testToSupplier_catchesRuntimeExceptionWithoutWrapping() {
Action action = () -> {
throw new IllegalArgumentException("some test exception");
};

Supplier<Void> supplier = action.toSupplier();

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(supplier::get)
.withMessage("some test exception")
.withNoCause();
}

@Test
void testToCallable() throws Exception {
Action action = numberOfExecutions::incrementAndGet;
Expand Down

0 comments on commit de7264b

Please sign in to comment.