相关疑难解决方法(0)

用于链接呼叫的模拟或存根

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.有什么想法让测试用例更简单吗?

java unit-testing mocking mockito

63
推荐指数
3
解决办法
5万
查看次数

Spock框架:间谍问题

我在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 …

java groovy unit-testing spock spock-spy

3
推荐指数
1
解决办法
4129
查看次数

标签 统计

java ×2

unit-testing ×2

groovy ×1

mocking ×1

mockito ×1

spock ×1

spock-spy ×1