日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

使用Mockito测试Spring组件

發布時間:2023/12/3 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Mockito测试Spring组件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我認為,能夠對彈簧組件進行單元測試而無需使用臨時測試配置加載完整的彈簧上下文,這是一個很大的優勢,因為它干凈,易于維護,編寫速度更快,更改平滑。

實現此目標的一種方法是使用Mockito并告訴他用Mocks (或Spies)替換您要測試的類中的@Autowired組件。

這里舉個例子。

我們有一個名為SalaryService的服務,它會根據傳遞的雇員ID來猜測是什么,從而計算出假設的凈工資。 簡單的概念。

協作者需要一項服務,第一個是EmployeeDAO,用于檢索工資總額,第二個是TaxCalculator,用于根據工資總額應用一些稅款。

package com.marco.springmockito; import java.math.BigDecimal; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class SalaryService {private static final BigDecimal minimumSalary = new BigDecimal(20000);@Autowiredprivate EmployeeDAO employeeDAO;@Autowiredprivate TaxCalculator taxCalculator;public BigDecimal getNetSalary(long employeeId) {BigDecimal netSalary = null;BigDecimal grossSalary = employeeDAO.getAnnualSalary(employeeId);BigDecimal taxes = taxCalculator.calculateTaxes(grossSalary);if (taxedSalaryIsGreaterThanMinimumSalary(grossSalary)) {netSalary = grossSalary.subtract(taxes);} else {netSalary = grossSalary;}return netSalary;}private boolean taxedSalaryIsGreaterThanMinimumSalary(BigDecimal taxedSalary) {return taxedSalary.compareTo(minimumSalary) == 1;} }

EmployeeDAO是一項經典服務,負責從持久性存儲中檢索信息,并且看起來或多或少都是這樣。

package com.marco.springmockito; import java.math.BigDecimal; import org.springframework.stereotype.Component; @Component public class EmployeeDAO {public BigDecimal getAnnualSalary(long employeeId) {// conncetTODB// run select for employeeId;return new BigDecimal(70000);} }

TaxCalculator將需要TaxDao來檢索稅收信息,然后它將進行某種無聊且冗長的計算以退還稅收。

package com.marco.springmockito; import java.math.BigDecimal; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class TaxCalculator {@Autowiredprivate TaxDao taxDao;public BigDecimal calculateTaxes(BigDecimal salary) {BigDecimal result = salary.multiply(taxDao.getTaxPercentageForYear(2014));// some other weird calculation ....return result;} }

現在,我們要對SalaryService類進行單元測試。 我們不應被DAO和任何種類的數據庫設置所困擾。 在此UNIT測試中,我們不在乎TaxCalculator在做什么。

我們想要的是測試我們的SalaryService行為是否符合預期,并且能夠正確使用其協作者的工作。

這就是我們如何使用Mockito做到這一點。 我們用@InjectMocks標記要測試的類,并用@Mock標記其所有協作者(如果需要真正的實現,則標記@Spy )。

最后,我們需要在開始測試之前告訴我們的Unit框架,以操作所需的Mockito注入,然后使用
MockitoAnnotations。 initMocks(this);。

在測試中,我們需要模擬預期的操作,以便我們可以集中精力在SalaryService中要測試的實際邏輯上。

package com.marco.springmockito; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.when; import java.math.BigDecimal; import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; public class SalaryServiceTest {private static final long UserId = 123l;@InjectMocksprivate SalaryService salaryService;@Mockprivate EmployeeDAO employeeDAO;@Mockprivate TaxCalculator taxCalculator;@Beforepublic void init() {MockitoAnnotations.initMocks(this);}@Testpublic void testMinimumSalary() {BigDecimal annualSalary = new BigDecimal(10000);when(employeeDAO.getAnnualSalary(UserId)).thenReturn(annualSalary);when(taxCalculator.calculateTaxes(annualSalary)).thenReturn(new BigDecimal(1000));BigDecimal actual = salaryService.getNetSalary(UserId);assertThat(actual.compareTo(new BigDecimal(10000)), is(0));}@Testpublic void testMaximumSalary() {BigDecimal annualSalary = new BigDecimal(80000);when(employeeDAO.getAnnualSalary(UserId)).thenReturn(annualSalary);when(taxCalculator.calculateTaxes(annualSalary)).thenReturn(new BigDecimal(8000));BigDecimal actual = salaryService.getNetSalary(UserId);assertThat(actual.compareTo(new BigDecimal(72000)), is(0));} }

它既簡單又高效,希望對其他人有用。

參考:從我們的JCG合作伙伴 Marco Castigliego的Mockito中測試Spring組件,位于“ 刪除重復并修復不良名稱”博客中。

翻譯自: https://www.javacodegeeks.com/2014/01/testing-spring-components-with-mockito.html

總結

以上是生活随笔為你收集整理的使用Mockito测试Spring组件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。