本文介绍了@Mock和@InjectMocks之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Mockito框架中的 @Mock
和 @InjectMocks
有什么区别?
What is the difference between @Mock
and @InjectMocks
in Mockito framework?
推荐答案
@Mock
创建一个模拟。 @InjectMocks
创建该类的实例并注入使用 @Mock
(或<$ c)创建的模拟$ c> @Spy )此实例的注释。
@Mock
creates a mock. @InjectMocks
creates an instance of the class and injects the mocks that are created with the @Mock
(or @Spy
) annotations into this instance.
请注意,您必须使用 @RunWith(MockitoJUnitRunner.class)
或 Mockito.initMocks (这)
初始化这些模拟并注入它们。
Note that you must use @RunWith(MockitoJUnitRunner.class)
or Mockito.initMocks(this)
to initialize these mocks and inject them.
@RunWith(MockitoJUnitRunner.class)
public class SomeManagerTest {
@InjectMocks
private SomeManager someManager;
@Mock
private SomeDependency someDependency; // this will be injected into someManager
//tests...
}
这篇关于@Mock和@InjectMocks之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!