@Mock和@InjectMocksMockito框架有什么区别?
@Mock
@InjectMocksMockito
@Mock创建一个模拟。@InjectMocks创建该类的实例,并将使用@Mock(或@Spy)注释创建的模拟注入该实例。
@InjectMocks
@Spy
请注意,你必须使用@RunWith(MockitoJUnitRunner.class)或Mockito.initMocks(this)初始化这些模拟并注入它们。
@RunWith(MockitoJUnitRunner.class)
Mockito.initMocks(this)
@RunWith(MockitoJUnitRunner.class) public class SomeManagerTest { @InjectMocks private SomeManager someManager; @Mock private SomeDependency someDependency; // this will be injected into someManager //tests... }