天天看点

@Mock和@InjectMocks之间的区别

本文翻译自:Difference between @Mock and @InjectMocks

Mockito框架中的

@Mock

@InjectMocks

什么区别?

#1楼

参考:https://stackoom.com/question/175zp/Mock和-InjectMocks之间的区别

#2楼

@Mock

creates a mock.

@Mock

创建了一个模拟。

@InjectMocks

creates an instance of the class and injects the mocks that are created with the

@Mock

(or

@Spy

) annotations into this instance.

@InjectMocks

创建该类的实例,并将使用

@Mock

(或

@Spy

)注释创建的

@Spy

注入此实例。

Note that you must use

@RunWith(MockitoJUnitRunner.class)

or

Mockito.initMocks(this)

to initialize these mocks and inject them.

请注意,您必须使用

@RunWith(MockitoJUnitRunner.class)

Mockito.initMocks(this)

来初始化这些

Mockito.initMocks(this)

并注入它们。
@RunWith(MockitoJUnitRunner.class)
public class SomeManagerTest {

    @InjectMocks
    private SomeManager someManager;

    @Mock
    private SomeDependency someDependency; // this will be injected into someManager

     //tests...

}
           

#3楼

@Mock

annotation mocks the concerned object.

@Mock

注释嘲笑有关对象。

@InjectMocks

annotation allows to inject into the underlying object the different (and relevant) mocks created by

@Mock

.

@InjectMocks

注释允许向底层对象注入由

@Mock

创建的不同(和相关)

@Mock

Both are complementary.

两者都是互补的。

#4楼

A "mocking framework", which Mockito is based on, is a framework that gives you the ability to create Mock objects ( in old terms these objects could be called shunts, as they work as shunts for dependend functionality ) In other words, a mock object is used to imitate the real object your code is dependend on, you create a proxy object with the mocking framework.

Mockito所基于的“模拟框架”是一个框架,它使您能够创建Mock对象(用旧的术语来说,这些对象可以被称为分流器,因为它们可以作为依赖功能的分流器)换句话说,一个模拟object用于模仿代码所依赖的真实对象,使用模拟框架创建代理对象。

By using mock objects in your tests you are essentially going from normal unit testing to integrational testing

通过在测试中使用模拟对象,您实际上是从正常的单元测试到集成测试

Mockito is an open source testing framework for Java released under the MIT License, it is a "mocking framework", that lets you write beautiful tests with clean and simple API.

Mockito是一个在MIT许可下发布的Java开源测试框架,它是一个“模拟框架”,可以让你用干净简单的API编写漂亮的测试。

There are many different mocking frameworks in the Java space, however there are essentially two main types of mock object frameworks, ones that are implemented via proxy and ones that are implemented via class remapping.

Java空间中有许多不同的模拟框架,但基本上有两种主要类型的模拟对象框架,一种是通过代理实现的,另一种是通过类重映射实现的。

Dependency injection frameworks like Spring allow you to inject your proxy objects without modifying any code, the mock object expects a certain method to be called and it will return an expected result.

像Spring这样的依赖注入框架允许您在不修改任何代码的情况下注入代理对象,模拟对象期望调用某个方法并返回预期结果。

The

@InjectMocks

annotation tries to instantiate the testing object instance and injects fields annotated with

@Mock

or

@Spy

into private fields of the testing object.

@InjectMocks

批注尝试实例化测试对象实例,并将带有

@Mock

@Spy

注释的字段注入测试对象的私有字段。

MockitoAnnotations.initMocks(this)

call, resets testing object and re-initializes mocks, so remember to have this at your

@Before

/

@BeforeMethod

annotation.

MockitoAnnotations.initMocks(this)

调用,重置测试对象并重新初始化模拟,所以请记住在

@Before

/

@BeforeMethod

注释中使用它。

#5楼

One advantage you get with the approach mentioned by @Tom is that you don't have to create any constructors in the SomeManager, and hence limiting the clients to instantiate it.

使用@Tom提到的方法获得的一个优点是您不必在SomeManager中创建任何构造函数,因此限制客户端实例化它。
@RunWith(MockitoJUnitRunner.class)
public class SomeManagerTest {

    @InjectMocks
    private SomeManager someManager;

    @Mock
    private SomeDependency someDependency; // this will be injected into someManager

    //You don't need to instantiate the SomeManager with default contructor at all
   //SomeManager someManager = new SomeManager();    
   //Or SomeManager someManager = new SomeManager(someDependency);

     //tests...

}
           

Whether its a good practice or not depends on your application design.

是否良好实践取决于您的应用程序设计。

#6楼

In your test class, the tested class should be annotated with

@InjectMocks

.

在测试类中,测试类应使用

@InjectMocks

进行注释。

This tells Mockito which class to inject mocks into:

这告诉Mockito哪个类注入模拟:
@InjectMocks
private SomeManager someManager;
           

From then on, we can specify which specific methods or objects inside the class, in this case,

SomeManager

, will be substituted with mocks:

从那时起,我们可以指定类中的哪些特定方法或对象,在本例中,

SomeManager

将被mocks替换:
@Mock
private SomeDependency someDependency;
           

In this example,

SomeDependency

inside the

SomeManager

class will be mocked.

在这个例子中,

SomeDependency

里面

SomeManager

类将被嘲笑。