Mockito thenReturn returns same instance

后端 未结 3 431
孤街浪徒
孤街浪徒 2021-02-04 00:03

I have this in Mockito:

when(mockedMergeContext.createNewEntityOfType(IService.class)).thenReturn(new ServiceMock());
         


        
3条回答
  •  粉色の甜心
    2021-02-04 00:17

    You might want to refactor that into different statements to understand why that happens.

    Service svc = new ServiceMock();
    when(mockedMergeContext.createNewEntityOfType(IService.class)).thenReturn( svc );
    

    Do you see now why it doesn't work? :)

    It's always returning the instance hold in svc, it won't re-evaluate new ServiceMock() each time that the method is invoked.

提交回复
热议问题