Skip to content

Commit

Permalink
Improved Mocking to allow use to use the stream().count() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay authored and egonw committed Jan 1, 2022
1 parent f1248f8 commit d0afb59
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,7 @@ public int[] first() {
* @return number of matches
*/
public int count() {
// Note: doesn't work when mocked due to forEachRemaining
// return (int)stream().count();
int count = 0;
for (int[] m : iterable)
count++;
return count;
return (int)stream().count();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.util.Iterator;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

Expand All @@ -42,6 +43,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -293,7 +295,8 @@ public void count() throws Exception {
Iterator<int[]> iterator = mock(Iterator.class);
when(iterable.iterator()).thenReturn(iterator);
when(iterator.hasNext()).thenReturn(true, true, true, true, true, false);
when(iterator.next()).thenReturn(new int[0]);
doCallRealMethod().when(iterator)
.forEachRemaining(ArgumentMatchers.any(Consumer.class));

Mappings ms = new Mappings(mock(IAtomContainer.class), mock(IAtomContainer.class), iterable);
assertThat(ms.count(), is(5));
Expand All @@ -306,6 +309,8 @@ public void countUnique() throws Exception {
Iterator<int[]> iterator = mock(Iterator.class);
when(iterable.iterator()).thenReturn(iterator);
when(iterator.hasNext()).thenReturn(true, true, true, true, false);
doCallRealMethod().when(iterator)
.forEachRemaining(ArgumentMatchers.any(Consumer.class));

int[] p1 = {0, 1, 2};
int[] p2 = {0, 2, 1};
Expand Down

0 comments on commit d0afb59

Please sign in to comment.