Skip to content

Commit d0afb59

Browse files
johnmayegonw
authored andcommitted
Improved Mocking to allow use to use the stream().count() method.
1 parent f1248f8 commit d0afb59

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

base/isomorphism/src/main/java/org/openscience/cdk/isomorphism/Mappings.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,12 +534,7 @@ public int[] first() {
534534
* @return number of matches
535535
*/
536536
public int count() {
537-
// Note: doesn't work when mocked due to forEachRemaining
538-
// return (int)stream().count();
539-
int count = 0;
540-
for (int[] m : iterable)
541-
count++;
542-
return count;
537+
return (int)stream().count();
543538
}
544539

545540
/**

tool/smarts/src/test/java/org/openscience/cdk/isomorphism/MappingsTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.util.Iterator;
3434
import java.util.Map;
35+
import java.util.function.Consumer;
3536
import java.util.function.Function;
3637
import java.util.function.Predicate;
3738

@@ -42,6 +43,7 @@
4243
import static org.junit.Assert.assertFalse;
4344
import static org.junit.Assert.assertTrue;
4445
import static org.mockito.Mockito.atMost;
46+
import static org.mockito.Mockito.doCallRealMethod;
4547
import static org.mockito.Mockito.mock;
4648
import static org.mockito.Mockito.verify;
4749
import static org.mockito.Mockito.when;
@@ -293,7 +295,8 @@ public void count() throws Exception {
293295
Iterator<int[]> iterator = mock(Iterator.class);
294296
when(iterable.iterator()).thenReturn(iterator);
295297
when(iterator.hasNext()).thenReturn(true, true, true, true, true, false);
296-
when(iterator.next()).thenReturn(new int[0]);
298+
doCallRealMethod().when(iterator)
299+
.forEachRemaining(ArgumentMatchers.any(Consumer.class));
297300

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

310315
int[] p1 = {0, 1, 2};
311316
int[] p2 = {0, 2, 1};

0 commit comments

Comments
 (0)