Class ReturnsElementsOf

  • All Implemented Interfaces:
    Answer<java.lang.Object>

    public class ReturnsElementsOf
    extends java.lang.Object
    implements Answer<java.lang.Object>
    Returns elements of the collection. Keeps returning the last element forever. Might be useful on occasion when you have a collection of elements to return.

    
       //this:
       when(mock.foo()).thenReturn(1, 2, 3);
       //is equivalent to:
       when(mock.foo()).thenAnswer(new ReturnsElementsOf(Arrays.asList(1, 2, 3)));
     

    Also you might better want to use the static factory there AdditionalAnswers.returnsElementsOf(java.util.Collection)

    See Also:
    AdditionalAnswers
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.LinkedList<java.lang.Object> elements  
    • Constructor Summary

      Constructors 
      Constructor Description
      ReturnsElementsOf​(java.util.Collection<?> elements)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object answer​(InvocationOnMock invocation)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • elements

        private final java.util.LinkedList<java.lang.Object> elements
    • Constructor Detail

      • ReturnsElementsOf

        public ReturnsElementsOf​(java.util.Collection<?> elements)
    • Method Detail

      • answer

        public java.lang.Object answer​(InvocationOnMock invocation)
                                throws java.lang.Throwable
        Specified by:
        answer in interface Answer<java.lang.Object>
        Parameters:
        invocation - the invocation on the mock.
        Returns:
        the value to be returned
        Throws:
        java.lang.Throwable - the throwable to be thrown