jeudi 13 septembre 2018

Mockito.when calling with real object return null, must use any()

When I unit test with a mock @Service, I notice that in the Mockito.when() statement, when I save the real object, I get null as return; and I have to use any().

So, instead of using:

@Mock
private BinInfoService service;
...  
@Test
public void testSave() {
    SomeBean bean = new SomeBean();
    Mockito.when(service.saveBinInfo(bean).thenReturn(bean);
}

I have to use:

Mockito.when(service.saveBinInfo(Mockito.any(SomeBean.class))).thenReturn(bean);

The first form returns null. While the second form returns the saved entity.

Why?

Aucun commentaire:

Enregistrer un commentaire