@Mock和@InjectMocks之间的区别

本文详细介绍了Mockito框架中@Mock和@InjectMocks注解的区别。@Mock用于创建模拟对象,而@InjectMocks则会创建类实例并将模拟对象注入其中。两者结合使用,可以在单元测试中方便地模拟依赖关系,简化测试代码。文章还探讨了模拟框架在单元测试和集成测试中的作用,以及Mockito与其他模拟框架的差异。
摘要由CSDN通过智能技术生成

本文翻译自: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类将被嘲笑。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值