以模拟方法内部的spring方面around方法调用



我想模拟@Aspect类内部的方法调用。

我有课,学生。

public class Student{
public String getName()
{
//
}
}
I have an Aspect class 
@Aspect
@Component
public class StudentAspect{
@Autowired
B b;
@Around( // the Student class get method)
public void around(ProceedingJoinPoint joinPoint)
{
b.doSomething();
}
}

我想用Mockito测试StudentAspect。我用程序为Student类创建了一个代理,这样我就可以触发StudentAspect类。但是,我不能模拟b类对象。有人能在这里帮忙吗。

您可以使用AspectJProxyFactory来测试方面。基于AspectJ的代理工厂,允许以编程方式构建包括AspectJ方面的代理。

示例:

@Test
public void testStudentAspect() {
B testBean = new B();
AspectJProxyFactory factory = new AspectJProxyFactory();
factory.setTarget(testBean);
CounterAspect myCounterAspect = new CounterAspect();
factory.addAspect(myCounterAspect);
ITestBean proxyTestBean = factory.getProxy();
proxyTestBean.doSomething();
//assert
}

最新更新