2016-06-01 3 views
0

インタフェースを含むこのJavaコードがあります。インタフェース付きモックforループ

ILocation<ILocation> contactLocations = fileSystem.getContactLocations(); 
for (ILocation location : contactLocations) { 
    location.getType(); 
    ... 
} 

public interface ILocation{ 
    String getName(); 
    ... 
} 

public interface ILocationS<T extends ILocation> extends IListCollectionWrapper<T> { 
    ... 
} 

public abstract interface IListCollectionWrapper<E>extends ICollectionWrapper<E>{ 
    public abstract List<E> getAsList(); 
} 

ILocation<ILocation> contactLocations = fileSystem.getContactLocations(); 
for (ILocation locations : contactLocations) { 
    ... 
} 

私はMockitoを使用して、forループの内容をテストしようとします。

Iterator<ILocation> mockIterator = mock(Iterator.class); 
Mockito.when(locations.iterator()).thenReturn(mockIterator); 

Mockito.when(mockIterator.next()).thenReturn(location); 

Mockito.when(location.getType()).thenReturn("test"); 

私はあなたが適切にイテレータの他の重要な方法、hasNextを嘲笑していないループ

+0

独自のイテレータを作成しましたか?もしそうなら、あなたはそのコードを共有することができますか?そうでなければ、適切な 'ILocation'オブジェクトを返すために' fileSystem.getContactLocations() 'をモックして、イテレータをモックする必要はありませんか? – FriedSaucePots

答えて

0

のためのデを入力することはありません。デフォルトでは、Mockitoはブール型のすべてのメソッドに対してfalseを返すので、hasNextは常にfalseになり、forループを決して入力しません。

これは、ほとんどのイテラブルのようなデータオブジェクトを模倣しない優れた理由です。実際のものを使用するか、new ArrayList<ILocation>を作成し、それを入力して、あなたのモックをyourArrayList.iterator()に返します。

関連する問題