It's not so easy.
You need to use something like powermock .
With powermock created, create a script before the method is called and plays it, which means that you can tell the constructor of the ArrayList class to ArrayList for the call and return a mock , not a real ArrayList .
This will allow you to claim on mock .
Something like this should work:
ArrayList listMock = createMock(ArrayList.class); expectNew(ArrayList.class).andReturn(listMock);
So, when your method creates a local List , powermock will return your mock List .
More details here .
This mix is ββvalid for unit testing outdated code that was not written for verification. I would highly recommend rewriting the code if possible so that such complex bullying should not occur.
source share