【问题标题】:Spring + lombok + @SneakyThrows春天 + 龙目岛 + @SneakyThrows
【发布时间】:2018-04-11 13:13:49
【问题描述】:

我在我的SpringBoot 项目中使用@SneakyThrows Lombok 功能。 当 CGLIB 代理实现时,我遇到了这个功能的问题 它抛出 java.lang.Exception: 意外异常, 预期但是
有办法解决吗?


提供示例。

有接口和两个实现。

public interface SneakyThrowsExample {
    void testSneakyThrows();
}

简单的实现

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();
    }
}

@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();
    }
}

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

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();
    }
}

测试 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

【问题讨论】:

  • 确实是一个非常有趣的问题。就个人而言,我只使用@SneakyThrows 来隐藏不可能的异常,因为我从未遇到过。当然不限于龙目岛。

标签: spring-boot lombok cglib


【解决方案1】:

当您使用 @Transactional 时,Spring 将通过 AOP 代理为您的 bean 创建一个代理 - Spring Framework’s declarative transaction

UndeclaredThrowableException 原因:

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

龙博克@SneakyThrows:

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

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

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

    @Test(expected = Exception.class)
        public void testSneakyThrowsTransactional() throws Exception {
            this.transactionalSneakyThrowsExample.testSneakyThrows();
    }

或者你可以在你的测试中使用ExpectedException.expectCause()来检查IOException.class,看看JUnit expect a wrapped exception

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 2021-10-02
    • 2021-03-27
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 2011-06-19
    相关资源
    最近更新 更多