DI依賴注入,mockito模擬依賴注入_Mockito @InjectMocks –模擬依賴注入

 2023-11-19 阅读 27 评论 0

摘要:mockito模擬依賴注入DI依賴注入,Mockito @InjectMocks annotations allow us to inject mocked dependencies in the annotated class mocked object. This is useful when we have external dependencies in the class we want to mock. We can specify the mock object

mockito模擬依賴注入

DI依賴注入,Mockito @InjectMocks annotations allow us to inject mocked dependencies in the annotated class mocked object. This is useful when we have external dependencies in the class we want to mock. We can specify the mock objects to be injected using @Mock or @Spy annotations.

Mockito @InjectMocks批注允許我們將模擬的依賴項注入到帶注釋的類模擬對象中。 當我們要模擬的類中具有外部依賴項時,這很有用。 我們可以使用@Mock或@Spy批注指定要注入的模擬對象。

Mockito @InjectMocks (Mockito @InjectMocks)

Maven依賴、Mockito tries to inject mocked dependencies using one of the three approaches, in the specified order.

Mockito嘗試使用三種方法之一以指定的順序注入模擬的依賴項。

  1. Constructor Based Injection – when there is a constructor defined for the class, Mockito tries to inject dependencies using the biggest constructor.

    基于構造函數的注入–當為該類定義了構造函數時,Mockito嘗試使用最大的構造函數注入依賴項。
  2. Setter Methods Based – when there are no constructors defined, Mockito tries to inject dependencies using setter methods.

    基于Setter方法-當未定義構造函數時,Mockito嘗試使用setter方法注入依賴項。
  3. Field Based – if there are no constructors or field-based injection possible, then mockito tries to inject dependencies into the field itself.

    基于字段的–如果沒有可能的構造函數或基于字段的注入,則Mockito嘗試將依賴項注入字段本身。

If there is only one matching mock object, then mockito will inject that into the object. If there is more than one mocked object of the same class, then mock object name is used to inject the dependencies.

如果只有一個匹配的模擬對象,則模擬將把它注入到對象中。 如果同一類的模擬對象不止一個,則使用模擬對象名稱來注入依賴項。

模擬@InjectMocks示例 (Mock @InjectMocks Example)

Let’s create some services and classes with dependencies so that we can see Mockito dependency injection of mocks in action.

讓我們創建一些具有依賴項的服務和類,以便我們可以看到Mockito依賴項在模擬中的依賴注入。

Service Classes

服務等級

package com.journaldev.injectmocksservices;public interface Service {public boolean send(String msg);
}
package com.journaldev.injectmocksservices;public class EmailService implements Service {@Overridepublic boolean send(String msg) {System.out.println("Sending email");return true;}}
package com.journaldev.injectmocksservices;public class SMSService implements Service {@Overridepublic boolean send(String msg) {System.out.println("Sending SMS");return true;}
}

App Service Classes with Dependencies

具有依賴關系的App Service類

package com.journaldev.injectmocksservices;//For Constructor Based @InjectMocks injection
public class AppServices {private EmailService emailService;private SMSService smsService;public AppServices(EmailService emailService, SMSService smsService) {this.emailService = emailService;this.smsService = smsService;}public boolean sendSMS(String msg) {return smsService.send(msg);}public boolean sendEmail(String msg) {return emailService.send(msg);}
}
package com.journaldev.injectmocksservices;//For Property Setter Based @InjectMocks injection
public class AppServices1 {private EmailService emailService;private SMSService smsService;public void setEmailService(EmailService emailService) {this.emailService = emailService;}public void setSmsService(SMSService smsService) {this.smsService = smsService;}public boolean sendSMS(String msg) {return smsService.send(msg);}public boolean sendEmail(String msg) {return emailService.send(msg);}}
package com.journaldev.injectmocksservices;//For Field Based @InjectMocks injection
public class AppServices2 {private EmailService emailService;private SMSService smsService;public boolean sendSMS(String msg) {return smsService.send(msg);}public boolean sendEmail(String msg) {return emailService.send(msg);}}

@InjectMocks構造函數注入示例 (@InjectMocks Constructor Injection Example)

package com.journaldev.mockito.injectmocks;import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;import com.journaldev.injectmocksservices.AppServices;
import com.journaldev.injectmocksservices.AppServices1;
import com.journaldev.injectmocksservices.AppServices2;
import com.journaldev.injectmocksservices.EmailService;
import com.journaldev.injectmocksservices.SMSService;class MockitoInjectMocksExamples extends BaseTestCase {@Mock EmailService emailService;@Mock SMSService smsService;@InjectMocks AppServices appServicesConstructorInjectionMock;@InjectMocks AppServices1 appServicesSetterInjectionMock;@InjectMocks AppServices2 appServicesFieldInjectionMock;@Testvoid test_constructor_injection_mock() {when(appServicesConstructorInjectionMock.sendEmail("Email")).thenReturn(true);when(appServicesConstructorInjectionMock.sendSMS(anyString())).thenReturn(true);assertTrue(appServicesConstructorInjectionMock.sendEmail("Email"));assertFalse(appServicesConstructorInjectionMock.sendEmail("Unstubbed Email"));assertTrue(appServicesConstructorInjectionMock.sendSMS("SMS"));}
}

Did you noticed that my test class is extending BaseTestCase. This is to initialize Mockito mocks before the tests, here is the code of the class.

您是否注意到我的測試類正在擴展BaseTestCase 。 這是在測試之前初始化Mockito模擬,這是該類的代碼。

package com.journaldev.mockito.injectmocks;import org.junit.jupiter.api.BeforeEach;
import org.mockito.MockitoAnnotations;class BaseTestCase {@BeforeEachvoid init_mocks() {MockitoAnnotations.initMocks(this);}}

If you won’t call MockitoAnnotations.initMocks(this); then you will get NullPointerException.

如果您不致電MockitoAnnotations.initMocks(this); 那么您將獲得NullPointerException

Also, I am using JUnit 5 to run the test cases. If you are not familiar with it, have a look at JUnit 5 Tutorial.

另外,我正在使用JUnit 5運行測試用例。 如果您不熟悉它,請查看JUnit 5 Tutorial 。

@InjectMocks設置器方法注入示例 (@InjectMocks Setter Methods Injection Example)

@Test
void test_setter_injection_mock() {when(appServicesSetterInjectionMock.sendEmail("New Email")).thenReturn(true);when(appServicesSetterInjectionMock.sendSMS(anyString())).thenReturn(true);assertTrue(appServicesSetterInjectionMock.sendEmail("New Email"));assertFalse(appServicesSetterInjectionMock.sendEmail("Unstubbed Email"));assertTrue(appServicesSetterInjectionMock.sendSMS("SMS"));}

@InjectMocks基于字段的注入示例 (@InjectMocks Field Based Injection Example)

@Test
void test_field_injection_mock() {when(appServicesFieldInjectionMock.sendEmail(anyString())).thenReturn(true);when(appServicesFieldInjectionMock.sendSMS(anyString())).thenReturn(true);assertTrue(appServicesFieldInjectionMock.sendEmail("Email"));assertTrue(appServicesFieldInjectionMock.sendEmail("New Email"));assertTrue(appServicesFieldInjectionMock.sendSMS("SMS"));}
GitHub Repository.GitHub存儲庫中查看完整的代碼和更多Mockito示例。

Reference: API Doc

參考: API文檔

翻譯自: https://www.journaldev.com/21887/mockito-injectmocks-mocks-dependency-injection

mockito模擬依賴注入

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/183254.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息