javascript
使用Mockito测试Spring组件
我認為,能夠?qū)椈山M件進行單元測試而無需使用臨時測試配置加載完整的彈簧上下文,這是一個很大的優(yōu)勢,因為它干凈,易于維護,編寫速度更快,更改平滑。
實現(xiàn)此目標的一種方法是使用Mockito并告訴他用Mocks (或Spies)替換您要測試的類中的@Autowired組件。
這里舉個例子。
我們有一個名為SalaryService的服務,它會根據(jù)傳遞的雇員ID來猜測是什么,從而計算出假設(shè)的凈工資。 簡單的概念。
協(xié)作者需要一項服務,第一個是EmployeeDAO,用于檢索工資總額,第二個是TaxCalculator,用于根據(jù)工資總額應用一些稅款。
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是一項經(jīng)典服務,負責從持久性存儲中檢索信息,并且看起來或多或少都是這樣。
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;} }現(xiàn)在,我們要對SalaryService類進行單元測試。 我們不應被DAO和任何種類的數(shù)據(jù)庫設(shè)置所困擾。 在此UNIT測試中,我們不在乎TaxCalculator在做什么。
我們想要的是測試我們的SalaryService行為是否符合預期,并且能夠正確使用其協(xié)作者的工作。
這就是我們?nèi)绾问褂肕ockito做到這一點。 我們用@InjectMocks標記要測試的類,并用@Mock標記其所有協(xié)作者(如果需要真正的實現(xiàn),則標記@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));} } 它既簡單又高效,希望對其他人有用。
翻譯自: https://www.javacodegeeks.com/2014/01/testing-spring-components-with-mockito.html
總結(jié)
以上是生活随笔為你收集整理的使用Mockito测试Spring组件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux命令关闭防火墙(linux命令
- 下一篇: DDOS攻击示意图(ddos攻击图示)