@Test
public void scratch(){
List<String> testlist = new ArrayList<String>(){{
add("1");
}};
Iterator<String> iter = mock(Iterator.class);
when(iter.hasNext()).thenReturn(true, false);
when(iter.next()).thenReturn("1");
List<String> resultList = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(iter, Spliterator.ORDERED), false)
.collect(Collectors.toList());
assertThat(resultList.size(), is(1));
assertThat(resultList.get(0), is("1"));
}
@Test
public void scratch(){
List<String> testlist = new ArrayList<String>(){{
add("1");
}};
List<String> resultList = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(testlist.iterator(), Spliterator.ORDERED), false)
.collect(Collectors.toList());
assertThat(resultList.size(), is(1));
assertThat(resultList.get(0), is("1"));
}
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+unsubscribe@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/3bb00070-98e5-4349-80f7-fa1e47e298e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I agree with Malte on this type of things. Don’t mock types you don’t own.
Technically the reason is that the Stream that uses the created Spliterator does not invoke hasNext() or next() directly ; instead it invokes the default method forEachRemaining, which is not mocked thus do nothing as it is a mock.
Adding this stub make the test work (mockito 2 beta) :
doAnswer(CALLS_REAL_METHODS).when(iter).forEachRemaining(any());
But in the end this behaviour is subject to JDK implementation which is not under your or our control. For that reason the preferred way would be to use a concrete type.
Cheers,
— Brice
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/CALS12-PK8Kfyh7JJEbOYj3iGfyD3ELcmaJ1NtM-KhYjtXifw%3DQ%40mail.gmail.com.
Thank Brice and Malte on the do not mock iterator, but unfortunately, I need to test this function.List<UserRole> getUserRolesWithWildcardAppName(
UserInfo.Username userID,
Application.Name applicationName
) {
List<UserRole> resultList;
try {
Result<com.intuit.wasabi.repository.cassandra.pojo.UserRole> result =
userRoleAccessor.getUserRolesByUserIdWithWildcardAppName(userID.getUsername());
resultList = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(iter, Spliterator.ORDERED), false)
.collect(Collectors.toList()); } catch (ReadTimeoutException | UnavailableException | NoHostAvailableException e) {
throw new RepositoryException("Could not retrieve permissions for user \"" + userID + "\" and application "
+ "\"" + applicationName + "\"", e);
}
return resultList;
}
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/3bb00070-98e5-4349-80f7-fa1e47e298e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/CALS12-PK8Kfyh7JJEbOYj3iGfyD3ELcmaJ1NtM-KhYjtXifw%3DQ%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+unsubscribe@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/4a17f838-739f-475a-a952-602da90e5f2f%40googlegroups.com.
Thank Brice and Malte on the do not mock iterator, but unfortunately, I need to test this function.
List<UserRole> getUserRolesWithWildcardAppName(
UserInfo.Username userID,
Application.Name applicationName
) {
List<UserRole> resultList;
try {
Result<com.intuit.wasabi.repository.cassandra.pojo.UserRole> result =
userRoleAccessor.getUserRolesByUserIdWithWildcardAppName(userID.getUsername());
resultList = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(iter, Spliterator.ORDERED), false)
.collect(Collectors.toList()); } catch (ReadTimeoutException | UnavailableException | NoHostAvailableException e) {
throw new RepositoryException("Could not retrieve permissions for user \"" + userID + "\" and application "
+ "\"" + applicationName + "\"", e);
}
return resultList;
}
On Friday, August 12, 2016 at 3:09:22 AM UTC-7, Brice wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/3bb00070-98e5-4349-80f7-fa1e47e298e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/CALS12-PK8Kfyh7JJEbOYj3iGfyD3ELcmaJ1NtM-KhYjtXifw%3DQ%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+unsubscribe@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/4a17f838-739f-475a-a952-602da90e5f2f%40googlegroups.com.