diff --git a/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/exceptionininitializererror/ExceptionInInitializerErrorUnitTest.java b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/exceptionininitializererror/ExceptionInInitializerErrorUnitTest.java new file mode 100644 index 0000000000..352d5ab50e --- /dev/null +++ b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/exceptionininitializererror/ExceptionInInitializerErrorUnitTest.java @@ -0,0 +1,55 @@ +package com.baeldung.exceptionininitializererror; + +import org.junit.Test; + +import java.lang.reflect.Constructor; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class ExceptionInInitializerErrorUnitTest { + + @Test + public void givenStaticVar_whenThrows_thenWrapsItInAnExceptionInInitializerError() { + assertThatThrownBy(StaticVar::new) + .isInstanceOf(ExceptionInInitializerError.class) + .hasCauseInstanceOf(RuntimeException.class); + } + + @Test + public void givenStaticBlock_whenThrows_thenWrapsItInAnExceptionInInitializerError() { + assertThatThrownBy(StaticBlock::new) + .isInstanceOf(ExceptionInInitializerError.class) + .hasCauseInstanceOf(ArithmeticException.class); + } + + private static class CheckedConvention { + + private static Constructor constructor; + + static { + try { + constructor = CheckedConvention.class.getDeclaredConstructor(); + } catch (NoSuchMethodException e) { + throw new ExceptionInInitializerError(e); + } + } + } + + private static class StaticVar { + + private static int state = initializeState(); + + private static int initializeState() { + throw new RuntimeException(); + } + } + + private static class StaticBlock { + + private static int state; + + static { + state = 42 / 0; + } + } +}