2013-07-03 2 views
0

JMock의 onConsecutiveCalls 메소드가 전달 된 매개 변수 목록의 끝에 도달하면 전달 된 첫 번째 동작으로 루프하도록 설정하는 방법이 있습니까? 아래 샘플 코드에서 모의를 true->false->true->Ad infinitum으로 반환하고 싶습니다.Jmock 루프 연속 호출

모의 설정 :

final MyService myServiceMocked = mockery.mock(MyService.class); 

mockery.checking(new Expectations() {{ 
    atLeast(1).of(myServiceMocked).doSomething(with(any(String.class)), with(any(String.class))); 
    will (onConsecutiveCalls(
    returnValue(true), 
    returnValue(false) 
)); 
}}); 

방법 전화 해봐요 방법 : 다음, 내 테스트 클래스에 다음을 추가하는 대신에 onRecurringConsecutiveCalls()를 호출하여이 문제를 해결 가지고

... 
for (String id:idList){ 
    boolean first = getMyService().doSomething(methodParam1, someString); 
    boolean second = getMyService().doSomething(anotherString, id); 
} 
... 

답변

0

onConsecutiveCalls().

추가 코드 :

/** 
* Recurring version of {@link org.jmock.Expectations#onConsecutiveCalls(Action...)} 
* When last action is executed, loops back to first. 
* @param actions Actions to execute. 
* @return An action sequence that will loop through the given actions. 
*/ 
public Action onRecurringConsecutiveCalls(Action...actions) { 
    return new RecurringActionSequence(actions); 
} 
/** 
* Recurring version of {@link org.jmock.lib.action.ActionSequence ActionSequence} 
* When last action is executed, loops back to first. 
* @author AnthonyW 
*/ 
public class RecurringActionSequence extends ActionSequence { 
    List<Action> actions; 
    Iterator<Action> iterator; 

    /** 
    * Recurring version of {@link org.jmock.lib.action.ActionSequence#ActionSequence(Action...) ActionSequence} 
    * When last action is executed, loops back to first. 
    * @param actions Actions to execute. 
    */ 
    public RecurringActionSequence(Action... actions) { 
     this.actions = new ArrayList<Action>(Arrays.asList(actions)); 
     resetIterator(); 
    } 

    @Override 
    public Object invoke(Invocation invocation) throws Throwable { 
     if (iterator.hasNext()) 
      return iterator.next().invoke(invocation); 
     else 
      return resetIterator().next().invoke(invocation); 
    } 

    /** 
    * Resets iterator to starting position. 
    * @return <code>this.iterator</code> for chain calls. 
    */ 
    private Iterator<Action> resetIterator() { 
     this.iterator = this.actions.iterator(); 
     return this.iterator; 
    } 
} 

참고 :이 코드는 JMock 2.1의 소스 코드를 기반으로합니다.