powermock教程,easymock教程_EasyMock教程

 2023-11-19 阅读 21 评论 0

摘要:easymock教程EasyMock is a java based mocking framework, which is used in conjunction with other unit testing frameworks such as JUnit and TestNG. EasyMock is very similar to Mockito mocking framework. EasyMock是基于Java的模擬框架,可與其他單元測試框

easymock教程

EasyMock is a java based mocking framework, which is used in conjunction with other unit testing frameworks such as JUnit and TestNG. EasyMock is very similar to Mockito mocking framework.

EasyMock是基于Java的模擬框架,可與其他單元測試框架(例如JUnit和TestNG)結合使用。 EasyMock與Mockito模擬框架非常相似。

powermock教程?EasyMock allows us to create mock objects of interfaces and concrete classes and specify their behaviors. Then we can use these mocks to write code to test our main classes. This way writing unit tests are simplified as we don’t need to care about external dependencies.

EasyMock允許我們創建接口和具體類的模擬對象并指定其行為。 然后,我們可以使用這些模擬程序編寫代碼來測試我們的主類。 這種方式簡化了編寫單元測試的過程,因為我們不需要關心外部依賴項。

EasyMock (EasyMock)

We can use EasyMock to create mock objects and then inject them into the classes we want to test. However, EasyMock has certain limitations too.

我們可以使用EasyMock創建模擬對象,然后將它們注入到我們要測試的類中。 但是,EasyMock也有某些限制。

  • EasyMock can’t mock final and private methods. If these methods are called on mock object, then normal methods will get called.

    EasyMock不能模擬final和private方法。 如果在模擬對象上調用了這些方法,則將調用普通方法。
  • EasyMock provides built-in behavior for equals(), toString(), hashCode() and finalize() methods. It means that we can’t record our own behavior for these methods.

    EasyMock為equals() ,toString(),hashCode()和finalize()方法提供內置行為。 這意味著我們無法記錄這些方法的行為。

EasyMock Maven依賴關系 (EasyMock Maven Dependencies)

hook編程?We can add the following dependencies to our maven project to use the EasyMock mocking framework.

我們可以將以下依賴項添加到我們的maven項目中,以使用EasyMock模擬框架。

<dependency><groupId>org.easymock</groupId><artifactId>easymock</artifactId><version>3.6</version><scope>test</scope>
</dependency>

EasyMock教程 (EasyMock Tutorial)

Let’s create some sample classes and services for mocking. We will use JUnit 5 for writing test cases along with EasyMock for creating mock objects.

讓我們創建一些示例類和服務來進行模擬。 我們將使用JUnit 5編寫測試用例,并使用EasyMock來創建模擬對象。

package com.journaldev.utils;public interface Calculator {int add(int x, int y);int multiply(int x, int y);
}
package com.journaldev.utils;public class MathUtils {private Calculator calc;public MathUtils(Calculator c) {this.calc = c;}public int add(int i, int j) {return this.calc.add(i, j);}public int multiply(int i, int j) {return this.calc.multiply(i, j);}
}

mockplus?Note that we don’t need to have Calculator implementation for writing our test cases because we are mocking them. This way we can apply the Test-Driven-Development (TDD) model in our application.

請注意,由于我們正在模擬測試用例,因此不需要使用Calculator實現即可編寫測試用例。 這樣,我們可以在我們的應用程序中應用測試驅動開發(TDD)模型。

Most of the useful methods are provided in the org.easymock.EasyMock class. Since they are mostly static, we can import them to write fluent and more readable code.

org.easymock.EasyMock類提供了大多數有用的方法。 由于它們大部分是靜態的,因此我們可以將其導入以編寫流利且更具可讀性的代碼。

easymock,We can also mock a concrete class and specify its behaviors. Let’s first look at a simple example, where we will mock ArrayList class.

我們還可以模擬一個具體的類并指定其行為。 讓我們首先看一個簡單的示例,在這里我們將模擬ArrayList類。

package com.journaldev.easymock;import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;import java.util.ArrayList;import org.junit.jupiter.api.Test;public class EasyMockMethodExample {@Testpublic void test() {ArrayList<Integer> mockList = mock(ArrayList.class);expect(mockList.add(10)).andReturn(true);expect(mockList.add(20)).andReturn(true);expect(mockList.size()).andReturn(2);expect(mockList.get(0)).andReturn(10);replay(mockList);mockList.add(10);mockList.add(20);// below will throw exception because it's not mocked// mockList.add(30);assertTrue(mockList.get(0) == 10);assertEquals(mockList.size(), 2);}
}

Some important points to note from above EasyMock example are:

在EasyMock示例上面需要注意的一些重要點是:

  • First step is to create mock object using EasyMock mock() method. This method is overloaded where we can specify if we want our mock object to be nice or strict. We can also specify name to be used incase of logging exceptions. We will look into different MockType examples in future posts.

    第一步是使用EasyMock mock()方法創建模擬對象。 這個方法是重載的 ,在這里我們可以指定我們想要的模擬對象是好還是嚴格。 我們還可以指定在記錄異常的情況下使用的名稱。 在以后的文章中,我們將研究不同的MockType示例。
  • Next step is to stub the behavior and output of a method call using expect() and andReturn() chain statements.

    下一步是末梢使用方法調用的行為和輸出expect()andReturn()鏈語句。
  • Finally, we have to call replay() method to start using stub methods on mock objects.

    最后,我們必須調用replay()方法以開始在模擬對象上使用存根方法。
  • If we call a method that is not stubbed, we will get java.lang.AssertionError.

    如果我們調用未存根的方法,則將得到java.lang.AssertionError

EasyMock注釋– @ Mock,@ TestSubject (EasyMock Annotations – @Mock, @TestSubject)

easymock使用教程。EasyMock also provides few annotations to create mock objects.

EasyMock還提供了一些注釋來創建模擬對象。

  • @Mock: Used to specify a field to be mocked by EasyMock.

    @Mock :用于指定EasyMock要模擬的字段。
  • @TestSubject: Used with a field so that EasyMock will inject mocks created with @Mock on its fields.

    @TestSubject :與字段@TestSubject使用,以便EasyMock將在其字段上注入使用@Mock創建的模擬。

When using annotations, we have to use EasyMockRunner, EasyMockRule or EasyMockSupport.injectMocks(Object) to initialize and inject mock objects. Since we are using JUnit-5, we can’t use EasyMockRunner and EasyMockRule as they are not supported yet. So we will have to use EasyMockSupport.injectMocks(Object). We can call this method in @BeforeEach method.

使用注釋時,我們必須使用EasyMockRunnerEasyMockRuleEasyMockSupport.injectMocks(Object)來初始化和注入模擬對象。 由于我們使用的是JUnit-5,因此我們無法使用EasyMockRunner和EasyMockRule,因為它們尚不支持。 因此,我們將不得不使用EasyMockSupport.injectMocks(Object) 。 我們可以在@BeforeEach方法中調用此方法。

easymock部署?Here is the complete example of mocking objects using EasyMock annotations.

這是使用EasyMock注釋模擬對象的完整示例。

package com.journaldev.easymock;import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.*;import org.easymock.EasyMockSupport;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;import com.journaldev.utils.Calculator;
import com.journaldev.utils.MathUtils;public class EasyMockAnnotationExample {@Mockprivate Calculator mockCalculator;@TestSubjectprivate MathUtils mathUtils = new MathUtils(mockCalculator);@BeforeEachpublic void setup() {EasyMockSupport.injectMocks(this);}@Testpublic void test() {expect(mockCalculator.add(1, 1)).andReturn(2);expect(mockCalculator.multiply(10, 10)).andReturn(100);replay(mockCalculator);assertEquals(mathUtils.add(1, 1), 2);assertEquals(mathUtils.multiply(10, 10), 100);}
}

EasyMock迭代器樣式存根 (EasyMock Iterator Style Stubbing)

We can specify different behaviors for the same method calls by chaining them with times(). Let’s look at a simple example to understand this clearly.

我們可以通過將它們與times()鏈接起來,為相同的方法調用指定不同的行為。 讓我們看一個簡單的例子來清楚地理解這一點。

//Iterator Style Mocking
reset(mockList);
expect(mockList.size()).andReturn(1).times(2) //return 1 for first 2 calls.andReturn(2).times(1) //return 2 for 3rd call.andReturn(4); //return 4 for 4th call
replay(mockList);assertEquals(mockList.size(), 1);
assertEquals(mockList.size(), 1);
assertEquals(mockList.size(), 2);
assertEquals(mockList.size(), 4);

摘要 (Summary)

easymock用法,EasyMock is a simple tool to create mock objects and write unit tests easily. It’s very similar to Mockito and provides almost the same features as Mockito.

EasyMock是一個簡單的工具,可以輕松創建模擬對象并編寫單元測試。 它與Mockito非常相似,并提供與Mockito幾乎相同的功能。

GitHub Repository.GitHub存儲庫中檢出完整的項目和更多EasyMock示例。

Reference: Official User Guide

參考: 官方用戶指南

mockplus教程基礎,翻譯自: https://www.journaldev.com/22193/easymock-tutorial

easymock教程

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

原文链接:https://hbdhgg.com/5/182946.html

发表评论:

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

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

底部版权信息