简体   繁体   中英

NullPointerException with mockito 2

I get java.lang.NullPointerException with this following code:

@Mock
SomeClass retObj;

when(obj.getXXXX(anyString())).thenReturn(retObj);
// obj.getXXXX(null) shows null in other methods

It used to work fine in Mockito 1.

I have fixed it. It turns out Mockito 2.0 handles null differently. When you setup mock as

when(obj.getXXXX(anyString())).thenReturn(retObj);

But actually the call is obj.getXXXX(null) it does not return retObj . Instead it returns null . Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM