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());
}
Run Code Online (Sandbox Code Playgroud)
这是测试的方法,
Method targetMethod = ctx.getTargetMethod();
CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class);
Run Code Online (Sandbox Code Playgroud)
我必须模拟三个CacheContext,Method和CacheEnable.有什么想法让测试用例更简单吗?
我在Spock中使用Spy有一个问题,它要么不能正常工作,要么我的理解是错误的,所以我试图弄清楚这一点.考虑这段代码(Java):
public class CallingClass {
public String functionOne() {
//does stuff
return "one";
}
public String functionTwo() {
String one = functionOne();
return "some string " + one;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想测试functionTwo
调用functionOne
以及定义返回值的事实functionOne
(想象一下,如果functionOne
真的很复杂,我不想在我的测试中执行它只想要存根并将其设置为返回某个值).为此,我在Groovy中编写以下测试(使用Spock):
class CallingClassTest extends Specification {
def "check that functionTwo calls functionOne"() {
def c = Spy(CallingClass)
c.functionOne() >> "mocked function return"
when:
def s = c.functionTwo()
then:
1 * c.functionOne()
s == "some string mocked function return"
}
}
Run Code Online (Sandbox Code Playgroud)
为了做这样的事情,Spock要我拥有这个cglib …