From e85b2bef44ef9def97b2c61f8bc93e5c72108507 Mon Sep 17 00:00:00 2001 From: sagaofsilence Date: Sun, 9 Jun 2024 23:08:01 +0530 Subject: [PATCH] updated code --- .../functional/ExecutableTest.java | 54 +++++-------------- 1 file changed, 13 insertions(+), 41 deletions(-) diff --git a/junit/junit5/functional-interfaces/src/test/java/io/reflectoring/functional/ExecutableTest.java b/junit/junit5/functional-interfaces/src/test/java/io/reflectoring/functional/ExecutableTest.java index 5429d485..dab8a5e1 100644 --- a/junit/junit5/functional-interfaces/src/test/java/io/reflectoring/functional/ExecutableTest.java +++ b/junit/junit5/functional-interfaces/src/test/java/io/reflectoring/functional/ExecutableTest.java @@ -14,6 +14,15 @@ import org.opentest4j.AssertionFailedError; public class ExecutableTest { + private final List numbers = Arrays.asList(100L, 200L, 50L, 300L); + final Executable sorter = + () -> { + TimeUnit.SECONDS.sleep(2); + numbers.sort(Long::compareTo); + }; + private final Executable checkSorting = + () -> assertEquals(List.of(50L, 100L, 200L, 300L), numbers); + private final Executable noChanges = () -> assertEquals(List.of(100L, 200L, 50L, 300L), numbers); @ParameterizedTest @CsvSource({"1,1,2,Hello,H,bye,2,byebye", "4,5,9,Good,Go,Go,-10,", "10,21,31,Team,Tea,Stop,-2,"}) @@ -67,64 +76,27 @@ void testAssertThrowsWithExecutable() { @Test void testAssertTimeoutWithExecutable() { - List numbers = Arrays.asList(100L, 200L, 50L, 300L); - final int delay = 2; - final Executable checkSorting = () -> assertEquals(List.of(50L, 100L, 200L, 300L), numbers); assertAll( () -> assertThrows( - AssertionFailedError.class, - () -> - assertTimeout( - Duration.ofSeconds(1), - () -> { - TimeUnit.SECONDS.sleep(delay); - numbers.sort(Long::compareTo); - })), + AssertionFailedError.class, () -> assertTimeout(Duration.ofSeconds(1), sorter)), checkSorting); assertAll( - () -> - assertDoesNotThrow( - () -> - assertTimeout( - Duration.ofSeconds(5), - () -> { - TimeUnit.SECONDS.sleep(delay); - numbers.sort(Long::compareTo); - })), - checkSorting); + () -> assertDoesNotThrow(() -> assertTimeout(Duration.ofSeconds(5), sorter)), checkSorting); } @Test void testAssertTimeoutPreemptivelyWithExecutable() { - List numbers = Arrays.asList(100L, 200L, 50L, 300L); - final int delay = 2; - final Executable checkSorting = () -> assertEquals(List.of(50L, 100L, 200L, 300L), numbers); - final Executable noChanges = () -> assertEquals(List.of(100L, 200L, 50L, 300L), numbers); assertAll( () -> assertThrows( AssertionFailedError.class, - () -> - assertTimeoutPreemptively( - Duration.ofSeconds(1), - () -> { - TimeUnit.SECONDS.sleep(delay); - numbers.sort(Long::compareTo); - })), + () -> assertTimeoutPreemptively(Duration.ofSeconds(1), sorter)), noChanges); assertAll( - () -> - assertDoesNotThrow( - () -> - assertTimeoutPreemptively( - Duration.ofSeconds(5), - () -> { - TimeUnit.SECONDS.sleep(delay); - numbers.sort(Long::compareTo); - })), + () -> assertDoesNotThrow(() -> assertTimeoutPreemptively(Duration.ofSeconds(5), sorter)), checkSorting); } }