BAEL-5026: Sneaky Throws - refactoring and fixes (#12334)
* BAEL-5026: refactored code examples * BAEL-5026: fixed test
This commit is contained in:
parent
edc22b309a
commit
4d6f72f2a3
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.baeldung.exceptions.sneakythrows;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SneakyThrows {
|
||||
|
||||
|
||||
public static <E extends Throwable> 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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.exceptions.sneakythrows;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
public class SneakyThrowsExamples {
|
||||
|
||||
public static <E extends Throwable> 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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue