diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/sneakythrows/SneakyRunnable.java b/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/sneakythrows/SneakyRunnable.java deleted file mode 100644 index 06b587d0e0..0000000000 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/sneakythrows/SneakyRunnable.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.exceptions.sneakythrows; - -import lombok.SneakyThrows; - -public class SneakyRunnable implements Runnable { - - @SneakyThrows - public void run() { - try { - throw new InterruptedException(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - public static void main(String[] args) { - try { - new SneakyRunnable().run(); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/sneakythrows/SneakyThrows.java b/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/sneakythrows/SneakyThrows.java deleted file mode 100644 index e86ef53733..0000000000 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/sneakythrows/SneakyThrows.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.baeldung.exceptions.sneakythrows; - -import java.io.IOException; - -public class SneakyThrows { - - - public static void sneakyThrow(Throwable e) throws E { - throw (E) e; - } - - public static void throwsSneakyIOException() { - sneakyThrow(new IOException("sneaky")); - } - - - public static void main(String[] args) { - try { - throwsSneakyIOException(); - } catch (Exception ex) { - ex.printStackTrace(); - } - - } -} diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/sneakythrows/SneakyThrowsExamples.java b/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/sneakythrows/SneakyThrowsExamples.java new file mode 100644 index 0000000000..37f2e4ba9c --- /dev/null +++ b/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/sneakythrows/SneakyThrowsExamples.java @@ -0,0 +1,22 @@ +package com.baeldung.exceptions.sneakythrows; + +import java.io.IOException; + +import lombok.SneakyThrows; + +public class SneakyThrowsExamples { + + public static void sneakyThrow(Throwable e) throws E { + throw (E) e; + } + + public static void throwSneakyIOException() { + sneakyThrow(new IOException("sneaky")); + } + + @SneakyThrows + public static void throwSneakyIOExceptionUsingLombok() { + throw new IOException("lombok sneaky"); + } + +} diff --git a/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/sneakythrows/SneakyRunnableUnitTest.java b/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/sneakythrows/SneakyRunnableUnitTest.java deleted file mode 100644 index 086c4eaef0..0000000000 --- a/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/sneakythrows/SneakyRunnableUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.exceptions.sneakythrows; - -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class SneakyRunnableUnitTest { - - @Test - public void whenCallSneakyRunnableMethod_thenThrowException() { - try { - new SneakyRunnable().run(); - } catch (Exception e) { - assertEquals(InterruptedException.class, e.getStackTrace()); - } - } -} diff --git a/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/sneakythrows/SneakyThrowsExamplesUnitTest.java b/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/sneakythrows/SneakyThrowsExamplesUnitTest.java new file mode 100644 index 0000000000..8809bf78d9 --- /dev/null +++ b/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/sneakythrows/SneakyThrowsExamplesUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.exceptions.sneakythrows; + +import static com.baeldung.exceptions.sneakythrows.SneakyThrowsExamples.throwSneakyIOException; +import static com.baeldung.exceptions.sneakythrows.SneakyThrowsExamples.throwSneakyIOExceptionUsingLombok; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.IOException; + +import org.junit.Test; + +public class SneakyThrowsExamplesUnitTest { + + @Test + public void throwSneakyIOException_IOExceptionShouldBeThrown() { + assertThatThrownBy(() -> throwSneakyIOException()) + .isInstanceOf(IOException.class) + .hasMessage("sneaky") + .hasStackTraceContaining("SneakyThrowsExamples.throwSneakyIOException"); + } + + @Test + public void throwSneakyIOExceptionUsingLombok_IOExceptionShouldBeThrown() { + assertThatThrownBy(() -> throwSneakyIOExceptionUsingLombok()) + .isInstanceOf(IOException.class) + .hasMessage("lombok sneaky") + .hasStackTraceContaining("SneakyThrowsExamples.throwSneakyIOExceptionUsingLombok"); + } +} diff --git a/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/sneakythrows/SneakyThrowsUnitTest.java b/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/sneakythrows/SneakyThrowsUnitTest.java deleted file mode 100644 index 3b70128a9b..0000000000 --- a/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/sneakythrows/SneakyThrowsUnitTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.exceptions.sneakythrows; - -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class SneakyThrowsUnitTest { - - @Test - public void whenCallSneakyMethod_thenThrowSneakyException() { - try { - SneakyThrows.throwsSneakyIOException(); - } catch (Exception ex) { - assertEquals("sneaky", ex.getMessage().toString()); - } - } - -}