1

I am trying to verify if a method in all objects of an array is called once.

I am using powermockito and currently this is done by iterating through the objects

for(Layer layer : layers){
 verify(layer,times(1)).reset();
}

Is there a more efficient way to check if all objects in the array are called with the method once ? i.e. without iteration

1 Answer 1

1

I guess, you cannot check a condition for all objects in a collection or array without iteration. Maybe it is possible to hide this information behind a Mockito method call, but nevertheless, Mockito will do an iteration to check the condition for all your objects.

You could create a helper method doing the iteration if you wanted to look your test code to look more self-explanatory.

Or you could use Stream.forEach(Consumer) if you were using Java 8: http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach-java.util.function.Consumer-

Sign up to request clarification or add additional context in comments.

1 Comment

Would be handy if they had provided that in the API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.