title: Spring + lombok + @SneakyThrows
body: <p>I'm using <a href="https://projectlombok.org/features/SneakyThrows" rel="noreferrer">@SneakyThrows</a> <a href="https://projectlombok.org/" rel="noreferrer">Lombok</a> feature in my <a href="https://projects.spring.io/spring-boot/" rel="noreferrer">SpringBoot</a> project.
  I have problems with this feature when CGLIB proxies implementation
  it throws
  <strong>java.lang.Exception: Unexpected exception,
  expected but was&lt;java.lang.reflect.UndeclaredThrowableException&gt;</strong>.<br/>
  Can it be fixed somehow ?</p>
  
  <hr>
  
  
  
  <p>Providing examples.</p>
  
  <p>There is interface and two implementations.</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface SneakyThrowsExample {
      void testSneakyThrows();
  }
  </code></pre>
  
  <hr>
  
  <p>Simple implementation</p>
  
  <pre class="lang-java prettyprint-override"><code>import lombok.SneakyThrows;
  import org.springframework.stereotype.Component;
  
  import java.io.IOException;
  
  @Component(value = "simpleSneakyThrowsExample")
  public class SimpleSneakyThrowsExample implements SneakyThrowsExample {
  
      @Override
      @SneakyThrows
      public void testSneakyThrows() {
          throw new IOException();
      }
  }
  </code></pre>
  
  <hr>
  
  <p>And <strong>@Transactional</strong> implementations. CGLIB will proxy this implementation.</p>
  
  <pre class="lang-java prettyprint-override"><code>import lombok.SneakyThrows;
  import org.springframework.stereotype.Component;
  import org.springframework.transaction.annotation.Transactional;
  
  import java.io.IOException;
  
  @Component(value = "transactionalSneakyThrowsExample")
  public class TransactionalSneakyThrowsExample implements SneakyThrowsExample {
  
      @Override
      @SneakyThrows
      @Transactional
      public void testSneakyThrows() {
          throw new IOException();
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Create @SpringBootTest test and inject these 2 implementation </p>
  
  <pre class="lang-java prettyprint-override"><code>import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.junit4.SpringRunner;
  
  import java.io.IOException;
  
  @RunWith(SpringRunner.class)
  @SpringBootTest
  public class DefaultSneakyThrowsExampleTest {
  
      @Autowired
      @Qualifier(value = "transactionalSneakyThrowsExample")
      SneakyThrowsExample transactionalSneakyThrowsExample;
  
      @Autowired
      @Qualifier(value = "simpleSneakyThrowsExample")
      SneakyThrowsExample simpleSneakyThrowsExample;
  
      @Test(expected = IOException.class)
      public void testSneakyThrowsSimple() throws Exception {
          this.simpleSneakyThrowsExample.testSneakyThrows();
      }
  
      @Test(expected = IOException.class)
      public void testSneakyThrowsTransactional() throws Exception {
          this.transactionalSneakyThrowsExample.testSneakyThrows();
      }
  }
  </code></pre>
  
  <p>Test <strong><em>testSneakyThrowsTransactional</em></strong> fails with error</p>
  
  <pre class="lang-java prettyprint-override"><code>java.lang.Exception: Unexpected exception, expected&lt;java.io.IOException&gt; but was&lt;java.lang.reflect.UndeclaredThrowableException&gt;
  
      at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  Caused by: java.lang.reflect.UndeclaredThrowableException
      at fine.project.TransactionalSneakyThrowsExample$$EnhancerBySpringCGLIB$$57df642e.testSneakyThrows(&lt;generated&gt;)
      at fine.project.DefaultSneakyThrowsExampleTest.testSneakyThrowsTransactional(DefaultSneakyThrowsExampleTest.java:35)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
      ... 20 more
  Caused by: java.io.IOException
      at fine.project.TransactionalSneakyThrowsExample.testSneakyThrows(TransactionalSneakyThrowsExample.java:21)
      at fine.project.TransactionalSneakyThrowsExample$$FastClassBySpringCGLIB$$e5429d83.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
      at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
      ... 31 more
  </code></pre>