我在模拟与Iterable结合调用的类时遇到问题spliterator()。spliterator一次调用一切正常,但是第二次调用不返回任何值。由于模拟总是返回相同的Spliterator实例,因此我假设状态未重置。有没有办法做到这一点?这是我能给的最小的例子该呼叫mapStringToHash是现实生活中的Lib,无法更改。 MyIterable也不受我控制。package net.test;import static org.hamcrest.CoreMatchers.is;import static org.mockito.Mockito.doReturn;import static org.mockito.Mockito.mock;import static java.util.stream.Collectors.toList;import static org.hamcrest.MatcherAssert.assertThat;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;import java.util.Spliterator;import java.util.stream.Stream;import java.util.stream.StreamSupport;import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.runners.MockitoJUnitRunner;@RunWith(MockitoJUnitRunner.class)public class SpliteratorTest { class MyIterable<T> implements Iterable<T> { private List<T> list; MyIterable(List<T> list) { this.list = new ArrayList<>(list); } @Override public Iterator<T> iterator() { return list.iterator(); } @Override public Spliterator<T> spliterator() { return list.spliterator(); } } // this a library method private Stream<Integer> mapStringToHash(final MyIterable<String> myIterable) { return StreamSupport.stream(myIterable.spliterator(), false).map(String::hashCode); } @Test public void testSeveralSpliteratorCalls() { MyIterable myIterable = givenMyIterableMock("a", "b", "c"); Stream<Integer> myIterableHash1 = mapStringToHash(myIterable); assertThat(myIterableHash1.count(), is(3L)); Stream<Integer> myIterableHash2 = mapStringToHash(myIterable); assertThat(myIterableHash2.count(), is(3L)); }
添加回答
举报
0/150
提交
取消