JUnit,@ControllerAdvice和缺少Kotlin中检查的异常
我在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没有检查异常 。 我认为不仅我有这个问题 – 我非常确定在Spring和Java中使用@ControllerAdvice处理异常的方式也可能会在我们决定在生产项目中切换到Kotlin时产生问题 – 因为它依赖于抛出的类,抛出UndeclaredThrowableException
带走了我们这种可能性。 有没有办法绕过这种行为,以确保有可能切换到Kotlin和旧的 Spring处理异常的方式(和ValidationAdvisor
单元测试)将保持不变? 预先感谢您的任何帮助。
PS Github的项目回购可以在这里找到: PlayGroud Kotlin,Spring Data等 。
PS2 Stacktrace:
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
是@Around
春季数据存储库?
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> { }
你正在试图使save()
方法或你的仓库抛出一个没有声明的检查异常。 所以,即使方面声明,这也不能工作。 你需要让你的异常延长RuntimeException
。
- 如何在eclipse maven web项目中编译kotlin
- Kotlin与Spring DI:lateinit属性尚未初始化
- Spring MVC错误404错误的请求Kotlin
- 以编程方式重新启动Spring Boot应用程序/刷新Spring上下文
- 异常处理程序不能使用`spring-boot-starter-data-rest`
- 我不能在Springboot-Kotlin中排除MongoAutoConfiguration(MongoSocketOpenException)
- Spring Boot 2&Spring 5错误服务index.html
- 春天的数据mongodb和kotlin
- Kotlin弹簧安全配置