mock or stub for chained call

后端 未结 4 1759
不知归路
不知归路 2020-11-29 03:03
protected int parseExpire(CacheContext ctx) throws AttributeDefineException {
    Method targetMethod = ctx.getTargetMethod();
    CacheEnable cacheEnable = targetMe         


        
4条回答
  •  心在旅途
    2020-11-29 04:00

    Mockito can handle chained stubs:

    Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);
    
    // note that we're stubbing a chain of methods here: getBar().getName()
    when(mock.getBar().getName()).thenReturn("deep");
    
    // note that we're chaining method calls: getBar().getName()
    assertEquals("deep", mock.getBar().getName());
    

    AFAIK, the first method in the chain returns a mock, which is set up to return your value on the second chained method call.

    Mockito's authors note that this should only be used for legacy code. A better thing to do otherwise is to push the behavior into your CacheContext and provide any information it needs to do the job itself. The amount of information you're pulling from CacheContext suggests that your class has feature envy.

提交回复
热议问题