Java 单元测试中调用的AOP事后建议
说明 我有一个单元测试,我不希望加载AOP。我没有在任何单元测试代码中加载AOP。未模拟的类没有任何自动连接和/或使用的AOP/Bean/组件 在运行单元测试时,假设代码抛出一个名为FrameworkException的自定义异常 但是,AOP捕获异常并运行后续通知。我不想在单元测试中使用这个 有人能帮忙吗 问题 为什么仍然调用ExceptionAspect Afterhrowing()建议?以前没有叫过 怀疑 模拟代码是否仍然使用任何现有功能进行实例化 尝试的解决方案Java 单元测试中调用的AOP事后建议,java,unit-testing,spring-aop,Java,Unit Testing,Spring Aop,说明 我有一个单元测试,我不希望加载AOP。我没有在任何单元测试代码中加载AOP。未模拟的类没有任何自动连接和/或使用的AOP/Bean/组件 在运行单元测试时,假设代码抛出一个名为FrameworkException的自定义异常 但是,AOP捕获异常并运行后续通知。我不想在单元测试中使用这个 有人能帮忙吗 问题 为什么仍然调用ExceptionAspect Afterhrowing()建议?以前没有叫过 怀疑 模拟代码是否仍然使用任何现有功能进行实例化 尝试的解决方案 -我尝试加载一个Anno
-我尝试加载一个AnnotationConfigApplicationContext,它加载一个空的配置类。这似乎不起作用 代码示例-单元测试
@Test
public void processRequest_WithRequestParameterNull_ExceptionExpected()
{
try
{
RequestWrapper requestMock = Mockito.mock(RequestWrapper.class);
Auditor auditorMock = Mockito.mock(Auditor.class);
CoreWrapper coreMock = Mockito.mock(CoreWrapper.class);
RequestAssessmentStatusHandler handler = new RequestAssessmentStatusHandler(requestMock, auditorMock,
coreMock);
handler.processRequest(null);
fail("processRequest_WithRequestParameterNull_ExceptionExpected failed.");
}
catch (FrameworkException e)
{
assertEquals(EventIds.INVALID_FRAMEWORK_PARAMETER, e.getEventId());
}
catch (Exception e)
{
fail("processRequest_WithRequestParameterNull_ExceptionExpected unhandled exception: "
+ e.getStackTrace().toString());
}
}
代码示例-后续建议
@Component
@Aspect
public class ExceptionAspect
{
@AfterThrowing(pointcut = "execution(* *(..))", throwing = "exception")
public void afterThrowing(JoinPoint joinPoint, Exception exception) throws Exception
{
// Do things here.
}
}
您使用哪种编织方法?您编写的编译代码(.class)似乎已经使用方面进行了增强,因此您的单元测试只使用纯RequestAssessmentStatusHandler类(没有Spring上下文)从方面调用代码。当在运行时使用classloader加载类时,也许您可以将其切换到编织方面?@Szpak我该如何做您所指的事情?