How to return a list when unit testing DynamoDB PaginatedQueryList
<p>I have a query statement which returns a <code>PaginatedQueryList</code> and I assign it to a List. Then I operate on the list.</p>

<pre><code>List&lt;MyClass&gt; entries= mapper.query(MyClass.class, queryExpression);
//Logic to test
Map&lt;String, Map&gt; map = entries.stream()
                        .collect(Collectors.toMap(MyClass::getA, MyClass::getB));
....
....
</code></pre>

<p>I tried to test it like</p>

<pre><code>when(mapper.query(eq(MyClass.class), any(DynamoDBQueryExpression.class)))
                              .thenReturn((PaginatedQueryList&lt;MyClass&gt;) list);
</code></pre>

<p>where <code>list</code> is <code>List&lt;MyClass&gt;</code>(ArrayList) that I construct myself and want to pass it to the main program to test the main logic.</p>

<p>But it throws</p>

<blockquote>
  <p>java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList</p>
</blockquote>

<p>How can I send the list when it executes <code>mappper.query(..)</code>.</p>

<p>The motive is to send the list that I constructed to <code>entries</code> when executing <code>mapper.query(..)</code>.</p>

<p><strong>NOTE:</strong></p>

<p>I also tried sending a <code>mocked</code> PaginatedQueryList but when it does <code>entries.stream()</code> I want to send the actual stream.
I tried like</p>

<pre><code>when(paginatedQueryList.stream()).thenReturn(Stream.of(list.get(0), list.get(1)));
</code></pre>

<p>It throws</p>

<blockquote>
  <p>java.lang.IllegalStateException: stream has already been operated upon or closed
      at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:229)</p>
</blockquote>

<p><strong>NOTE 2:</strong></p>

<p>I use <code>entries.stream()</code> twice in my logic to test</p>