mockito - 在方法周围的弹簧方面模拟方法调用
问题描述
我想在 @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
}
推荐阅读
- python - 无法连接到 'localhost 上的 MySQL 服务器
- java - 了解 Java 9 中的 try-with-resources 增强
- sql - 具有多次迭代的 SQL CTE 递归
- java - 如何使用 apache.commons 中的 CSVParser 以任何顺序读取 CSV 列
- javascript - 如果我只有 ID 作为字符串,如何从数组中删除 objectId?
- python - 如何在 pandas 或 GeoPandas 中过滤具有无效几何形状的 WKT 字符串
- python - 类型错误:不能使用 re.findall() 在类似字节的对象上使用字符串模式
- java - WebViewClient loadData onPageFinished 没有被调用
- c# - 如何使用 PRISM、Mef、DI 将视图模型链接到选项卡控件内的用户控件?
- azure - 如何使用 Azure Python SDK 将 principal_id 转换为用户名