問題描述
Mockito.mock(Class<T> classToMock)
方法和@Mock
注解有什么區別?它們是一樣的嗎?
What is the difference between Mockito.mock(Class<T> classToMock)
method and the @Mock
annotation?
Are they the same?
例如,是這樣的:
private TestClass test = Mockito.mock(TestClass.class);
同:
@Mock
private TestClass test;
推薦答案
它們都達到了相同的結果.使用注解 (@Mock
) 通常被認為是更干凈",因為您不會用看起來都一樣的樣板賦值來填充代碼.
They both achieve the same result. Using an annotation (@Mock
) is usually considered "cleaner", as you don't fill up your code with boilerplate assignments that all look the same.
請注意,為了使用 @Mock
注釋,您的測試類應使用 @RunWith(MockitoJUnitRunner.class)
注釋或包含對 的調用MockitoAnnotations.initMocks(this)
在其 @Before
方法中.
Note that in order to use the @Mock
annotation, your test class should be annotated with @RunWith(MockitoJUnitRunner.class)
or contain a call to MockitoAnnotations.initMocks(this)
in its @Before
method.
這篇關于Mockito.mock(SomeClass) 和 @Mock 注釋有什么區別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!