Mockito thenReturn returns same instance

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

I have this in Mockito:

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


        
3条回答
  •  無奈伤痛
    2021-02-04 00:06

    In Java 8 with Lambdas you can just use

    when(mockedMergeContext.createNewEntityOfType(IService.class)).thenAnswer(invocation -> new ServiceMock());
    

    So just replace .thenReturn(new MyMock());

    with .thenAnswer(invocation -> new MyMock());

提交回复
热议问题