我在 Kotlin 中编写了一个验证顾问EntityValidationException
,它在验证失败时抛出:
@Aspect
@Named
class ValidationAdvisor
@Inject constructor(val validator: EntityValidator) {
@Around(EVERY_SAVE_AND_UPDATE_TO_DATABASE)
fun validate(point: ProceedingJoinPoint): Any {
val result: List<ConstraintViolation<Any>> = validator.validate(getEntity(point))
if (isEntityValid(result))
return point.proceed()
throw EntityValidationException(
violationInfos = result as List<ConstraintViolationInfo>
)
}
private fun getEntity(point: ProceedingJoinPoint): Any {
return point.args[0]
}
private fun isEntityValid(result: List<ConstraintViolation<Any>>): Boolean {
return result.isEmpty()
}
companion object {
const val EVERY_SAVE_AND_UPDATE_TO_DATABASE = "execution(* beans.repositories.BaseRepository.save(..))"
}
}
我有一个 JUnit 测试(我在 Kotlin 中编写了一些部分,在 Java 中编写了一些部分,以在现有的私有项目中学习 Kotlin),它检查是否引发了异常:
@Test(expected = EntityValidationException.class)
public void test_WhenNameIsNotPresent_ThenExceptionIsThrown() throws Exception {
repository.save(/* entity to save */);
}
但后来我看到测试失败了,因为它抛出UndeclaredThrowableException而不是EntityValidationException
. 接下来,我读到Kotlin 没有检查异常。我认为不仅我有这个问题——我很确定当我们决定在生产项目中切换到 Kotlin 时,在 Spring 和 Java 中使用 @ControllerAdvice 处理异常的方式也可能会出现问题——因为它依赖于抛出的类,并且扔掉UndeclaredThrowableException
我们这种可能性。有没有办法绕过这种行为,以确保可以切换到 Kotlin 并且旧的Spring 处理异常的方式(和单元测试ValidationAdvisor
)将保持不变?预先感谢您的任何帮助。
项目的PS Github 存储库可在此处获得:Playgroud for Kotlin、Spring Data 等。
PS2堆栈跟踪:
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:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy65.save(Unknown Source)
at ActorValidationTest.test_WhenNameIsNotPresent_ThenExceptionIsThrown(ActorValidationTest.java:26)
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:497)
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)
... 26 more
Caused by: beans.validator.exceptions.EntityValidationException
at beans.aop.validation.ValidationAdvisor.validate(ValidationAdvisor.kt:23)
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:497)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
... 37 more
对我来说看起来像EntityValidationException
(我想被抛出)原因UndeclaredThrowableException
。
EntityValidationException
Kotlin 类代码:
package beans.validator.exceptions
import beans.validator.constraintViolation.ConstraintViolationInfo
class EntityValidationException (val violationInfos: List<ConstraintViolationInfo>) : Exception() {
}
PS3这也没有用:
@Around(EVERY_SAVE_AND_UPDATE_TO_DATABASE)
@Throws(EntityValidationException::class)
fun validate(point: ProceedingJoinPoint): Any /* rest of code*/
PS4也许问题在于@Aspect
Spring @Around
Data 存储库?
companion object {
const val EVERY_SAVE_AND_UPDATE_TO_DATABASE = "execution(* beans.repositories.BaseRepository.save(..))"
}
....
@NoRepositoryBean
public interface BaseRepository<ENTITY, IDENTIFIER extends Serializable>
extends JpaRepository<ENTITY, IDENTIFIER>, JpaSpecificationExecutor<ENTITY> {
}