I ran into this problem when writing unit tests and took me a while to fix it. But still, I would like to understand what caused the discrepant behaviour:
1) Using Arrays.asList()
@Mock SomeClass obj;
List<SomeClass> list = Arrays.asList(obj);
This method doesn't work. It creates a list of size 1, with a null obj inside.
2) Adding mock during setup
@Mock SomeClass obj;
List<SomeClass> list = new ArrayList<>();
...
@Before
public void setup() {
list.add(obj);
}
This method works.
I'm just curious are the differences between the two methods, which might have caused one method to work but not the other?