protected int parseExpire(CacheContext ctx) throws AttributeDefineException {
Method targetMethod = ctx.getTargetMethod();
CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class);
ExpireExpr cacheExpire = targetMethod.getAnnotation(ExpireExpr.class);
// check for duplicate setting
if (cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE && cacheExpire != null) {
throw new AttributeDefineException("expire are defined both in @CacheEnable and @ExpireExpr");
}
// expire time defined in @CacheEnable or @ExpireExpr
return cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE ? cacheEnable.expire() : parseExpireExpr(cacheExpire, ctx.getArgument());
}
那是测试的方法,
Method targetMethod = ctx.getTargetMethod();
CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class);
我必须模拟三个 CacheContext、Method 和 CacheEnable。有什么想法可以使测试用例更简单吗?
原文由 jilen 发布,翻译遵循 CC BY-SA 4.0 许可协议
Mockito 可以处理链式存根:
AFAIK,链中的第一个方法返回一个模拟,它被设置为在第二个链式方法调用中返回您的值。
Mockito 的作者指出,这应该 _只用于遗留代码_。一个更好的做法是将行为推送到您的 CacheContext 中,并提供它自己完成工作所需的任何信息。您从 CacheContext 中提取的信息量表明您的班级具有 envy 功能。