This isn't altogether that easy.
You need to use something like powermock.
With powermock you create create a scenario before then method is called and play it back, this means you can tell the ArrayList
class constructor to anticipate being called and return a mock
rather than a real ArrayList
.
This would allow you to assert on the mock
.
Something like this ought to work:
ArrayList listMock = createMock(ArrayList.class);
expectNew(ArrayList.class).andReturn(listMock);
So when your method creates the local List
powermock will actually return your mock List
.
More information here.
This sort of mocking is really for unit testing of legacy code that wasn't written to be testable. I would strongly recommend, if you can, rewriting the code so that such complex mocking doesn't need to happen.