Spring + lombok + @SneakyThrows

Vol*_*man 7 cglib lombok spring-boot

我在SpringBoot项目中使用@SneakyThrows Lombok功能.当CGLIB代理实现它抛出java.lang.Exception时,我遇到此功能的问题 :预期的异常,但是<java.lang.reflect.UndeclaredThrowableException>. 它可以以某种方式修复吗?


提供示例.

有接口和两个实现.

public interface SneakyThrowsExample {
    void testSneakyThrows();
}
Run Code Online (Sandbox Code Playgroud)

简单的实施

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

@Transactional实现.CGLIB将代理此实现.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

创建@SpringBootTest测试并注入这两个实现

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

测试testSneakyThrowsTransactional失败并出错

java.lang.Exception: Unexpected exception, expected<java.io.IOException> but was<java.lang.reflect.UndeclaredThrowableException>

    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(<generated>)
    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(<generated>)
    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
Run Code Online (Sandbox Code Playgroud)

tsa*_*txt 3

当您使用@Transactional时,Spring 将通过 AOP 代理为您的 bean 创建代理 - Spring Framework\xe2\x80\x99s 声明性事务

\n\n

UndeclaredThrowableException原因:

\n\n
\n

如果代理实例的调用处理程序的 invoke 方法抛出一个已检查异常(不可分配给 RuntimeException 或 Error 的 Throwable),且该异常不可分配给在 throws 子句中声明的任何异常类型,则由代理实例上的方法调用引发在代理实例上调用并分派到调用处理程序的方法。

\n
\n\n

隆博克@SneakyThrows

\n\n
\n

可用于偷偷抛出已检查的异常,而无需在方法的 throws 子句中实际声明它

\n
\n\n

这意味着您TransactionalSneakyThrowsExample.testSneakyThrows()抛出已检查的异常(在throws方法签名中未声明),当实例包装在代理中时,这是非法行为

\n\n

在这种情况下,您可以将预期异常更改为Exception.class

\n\n
    @Test(expected = Exception.class)\n        public void testSneakyThrowsTransactional() throws Exception {\n            this.transactionalSneakyThrowsExample.testSneakyThrows();\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者您可以使用ExpectedException.expectCause()来检查IOException.class您的测试,看看JUnit 期望一个包装的异常

\n