Java Mockito.mock(SomeClass) 和 @Mock 注释有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23563615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Mockito.mock(SomeClass) and the @Mock annotation?
提问by Gábor Csikós
What is the difference between Mockito.mock(Class<T> classToMock)
method and the @Mock
annotation?
Are they the same?
Mockito.mock(Class<T> classToMock)
方法和@Mock
注解有什么区别?他们是一样的吗?
For Example, is this:
例如,这是:
private TestClass test = Mockito.mock(TestClass.class);
the same as:
等同于:
@Mock
private TestClass test;
采纳答案by Mureinik
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
) 通常被认为是“更干净的”,因为您不会用看起来都一样的样板分配来填充代码。
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.
请注意,为了使用@Mock
注释,您的测试类应该在其方法中使用注释@RunWith(MockitoJUnitRunner.class)
或包含对的调用。MockitoAnnotations.initMocks(this)
@Before
回答by geoand
The difference is in the lines of code you need to write :) :) :)
不同之处在于您需要编写的代码行 :) :) :)
Seriously though, using the annotations has the exact same effect as using the Mockito.mock.
不过说真的,使用注释与使用 Mockito.mock 具有完全相同的效果。
To quote the documentation of MockitoAnnotations
the use of annotations has the following benefits:
引用MockitoAnnotations
使用注解的文档有以下好处:
Allows shorthand creation of objects required for testing.
Minimizes repetitive mock creation code.
Makes the test class more readable.
Makes the verification error easier to read because field name is
used to identify the mock.
允许快速创建测试所需的对象。
最大限度地减少重复的模拟创建代码。
使测试类更具可读性。
使验证错误更易于阅读,因为字段名称
用于标识模拟。
The javadoc for MockitoAnnotations
is here
适用于JavadocMockitoAnnotations
是这里
回答by Shweta
They both are considered same and achieve the same thing but I'd prefer the second one:
它们都被认为是相同的并且实现了相同的目标,但我更喜欢第二个:
@Mock is an annotation which:
@Mock 是一个注释,它:
- Minimizes repetitive mock creation code.
- Makes the test class more readable.
- Allows shorthand creation of objects required for testing.
- 最大限度地减少重复的模拟创建代码。
- 使测试类更具可读性。
- 允许快速创建测试所需的对象。
回答by Dawood ibn Kareem
There are two significant advantages to using the annotation.
使用注释有两个显着优势。
- A mock created with
@Mock
can be injected into the class you're testing, using the@InjectMocks
annotation. This is a powerful technique that can make testing significantly easier. It just won't work with mocks created by themock
method. - If you have any errors involving your mock, the name of the mock will appear in the message. If you've used
@Mock
, then this name will just be the name of the field. This makes it really easy to find the problem mock.
@Mock
可以使用@InjectMocks
注释将创建的模拟注入到您正在测试的类中。这是一种强大的技术,可以显着简化测试。它只是不适用于该mock
方法创建的模拟。- 如果您的模拟有任何错误,则模拟的名称将出现在消息中。如果您使用了
@Mock
,则此名称将只是该字段的名称。这使得很容易找到问题模拟。
Of course, in addition to these two important advantages, most people find the @Mock
notation much more readable, and it does cut down on the amount of code. I see no reason not to use it.
当然,除了这两个重要的优点之外,大多数人发现这种@Mock
表示法更具可读性,并且确实减少了代码量。我认为没有理由不使用它。
回答by saferJo
The answer of the question is one big mistake. We just solved some problems caused by Mockito.mock(Your.class) as a field. We had few @Test methods. The 4th method was throwing an exception with 'thenThrow(ex)'. All of the @Test methods after it was failing and the reason was the exception thrown. They were sharing the mocked instance and the 'when' condition. After we changed from
这个问题的答案是一个大错误。我们刚刚解决了一些由 Mockito.mock(Your.class) 作为字段引起的问题。我们几乎没有@Test 方法。第 4 种方法使用“thenThrow(ex)”抛出异常。失败后的所有@Test 方法,原因是抛出异常。他们正在共享模拟实例和“何时”条件。在我们改变之后
TestClass testInstance = Mockito.mock(TestClass.class);
to
到
@Mock
TestClass testInstance;
everything started to work as expected. So Mockito.mock is creating a shared mock between the test methods, and @Mock does not.
一切都开始按预期工作。所以 Mockito.mock 在测试方法之间创建一个共享的 mock,而 @Mock 没有。