2014-09-25 39 views
0

任何人都可以确定为什么Mockito 1.9.5在此代码的'doesWhatIExpectItTo'测试中抛出一个UnfinishedStubbingException?Mockito 1.9.5抛出UnfinishedStubbingException

public interface Thing { 
    String getId(); 
    boolean isReady(); 
} 

public interface ThingCache { 
    Thing getThing(String theId); 
} 

private Set<String> getThingIdSet(int theSize){ 
    Set<String> thingIds = new HashSet<String>(); 
    for(int i = 0; i < theSize; i++) { 
     thingIds.add("thingId-" + i); 
    } 
    return thingIds; 
} 

private Thing getANewThing(String theId, boolean isReady) { 
    Thing theNewThing = mock(Thing.class); 
    when(theNewThing.getId()).thenReturn(theId); 
    when(theNewThing.isReady()).thenReturn(isReady); 
    return theNewThing; 
} 

@Test 
public void doesWhatIExpectItTo() { 
    ThingCache theCache = mock(ThingCache.class); 

    Set<String> thingIds = getThingIdSet(5); 
    for (String thingId : thingIds) { 
     when(theCache.getThing(thingId)).thenReturn(getANewThing(thingId, true)); 
    } 
} 

我已经试过各种备选方案,包括参数的匹配器和thenAnswer,通过SO未完成的磕碰异常的问题,我能找到运行,我似乎无法找到任何让过去这个基本问题。

似乎有一些简单/显而易见,我失踪。

回答

0

Doh!

问题是,我在.thenReturn()调用的上下文中调用getANewThing(),并且无法在该上下文中启动新的.when()调用。

答案是在ThingCache.getThing()方法调用.when()之前构造Thing模拟。

相关问题