I have this in Mockito:
when(mockedMergeContext.createNewEntityOfType(IService.class)).thenReturn(new ServiceMock());
The createNewEntityOfType method should always return a new ServiceMock instance but it returns twice the same reference.
Why the thenReturn method doesn’t return new ServiceMock?
The
thenReturnmethod will always return what is passed to it. The codenew Servicemock()is being executed prior to the call tothenReturn. The createdServiceMockis then being passed tothenReturn. ThereforethenReturnhas a absolute instance ofServiceMocknot a creation mechanism.If you need to provide an new instance, use thenAnswer