Multiple levels of @Mock and @InjectMocks

后端 未结 4 1396
星月不相逢
星月不相逢 2021-02-01 13:07

So I understand that in Mockito @InjectMocks will inject anything that it can with the annotation of @Mock, but how to handle this scenario?

@Mock
private MockOb         


        
4条回答
  •  没有蜡笔的小新
    2021-02-01 14:04

    This works for me:

    private MockObject1 mockObject1 = mock(MockObject1.class);
    
    @Spy
    private RealObject2 realObject = new RealObject2(mockObject1);
    
    @InjectMocks
    private SystemUnderTest systemUnderTest = new SystemUnderTest();
    

提交回复
热议问题