Search code examples
javalistmockitojava.util.concurrentsublist

How to test equality for a sublist in java, Mockito


I have a method that has a collection as an argument:

call(Collection<String> strings);

I am calling the method as follows:

myClass.call(list.sublist(1,n));

Everything is running perfectly when I am running the code. However, when testing with Mockito, I am testing it using the code snippet:

verify(myClass,times(1)).call(myList);

it is repeatedly throwing the following error:

Unable to evaluate the expression Method threw 'java.util.ConcurrentModificationException' exception.

I am guessing this is because it is unable to cast to test. Any workarounds that can help? I wany to check if myList contains same elements as those passed.


Solution

  • I got the answer. Actually, the verify method is called after the whole function has run.

    So even though myClass had correct sublist as a parameter when called, the sublist becomes invalid when list is modified later.

    Verify method is called after the whole method is called, so the sublist to be tested at the time of validation is invalid.

    Hence it was throwing that exception.