
- EasyMock 教程
- EasyMock - 首頁
- EasyMock - 概述
- EasyMock - 環境搭建
- EasyMock - 第一個應用
- EasyMock - JUnit 整合
- EasyMock - 新增行為
- EasyMock - 驗證行為
- EasyMock - 期望呼叫
- EasyMock - 多次呼叫
- EasyMock - 異常處理
- EasyMock - createMock
- EasyMock - createStrictMock
- EasyMock - createNiceMock
- EasyMock 有用資源
- EasyMock - 快速指南
- EasyMock - 有用資源
- EasyMock - 討論
EasyMock - 期望呼叫
EasyMock 對特定方法的呼叫次數進行特殊檢查。假設 MathApplication 應該只調用 CalculatorService.serviceUsed() 方法一次,那麼它不應該能夠多次呼叫 CalculatorService.serviceUsed()。
//add the behavior of calc service to add two numbers and serviceUsed. EasyMock.expect(calcService.add(10.0,20.0)).andReturn(30.00); calcService.serviceUsed(); //limit the method call to 1, no less and no more calls are allowed EasyMock.expectLastCall().times(1);
建立 CalculatorService 介面如下。
檔案: CalculatorService.java
public interface CalculatorService { public double add(double input1, double input2); public double subtract(double input1, double input2); public double multiply(double input1, double input2); public double divide(double input1, double input2); public void serviceUsed(); }
calcService.serviceUsed() 呼叫一次的示例
步驟 1:建立一個名為 CalculatorService 的介面,提供數學函式
檔案: CalculatorService.java
public interface CalculatorService { public double add(double input1, double input2); public double subtract(double input1, double input2); public double multiply(double input1, double input2); public double divide(double input1, double input2); public void serviceUsed(); }
步驟 2:建立一個 JAVA 類來表示 MathApplication
檔案: MathApplication.java
public class MathApplication { private CalculatorService calcService; public void setCalculatorService(CalculatorService calcService){ this.calcService = calcService; } public double add(double input1, double input2){ calcService.serviceUsed(); return calcService.add(input1, input2); } public double subtract(double input1, double input2){ return calcService.subtract(input1, input2); } public double multiply(double input1, double input2){ return calcService.multiply(input1, input2); } public double divide(double input1, double input2){ return calcService.divide(input1, input2); } }
步驟 3:測試 MathApplication 類
讓我們透過向其中注入 calculatorService 的模擬來測試 MathApplication 類。模擬將由 EasyMock 建立。
檔案: MathApplicationTester.java
import org.easymock.EasyMock; import org.easymock.EasyMockRunner; import org.easymock.Mock; import org.easymock.TestSubject; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; // @RunWith attaches a runner with the test class to initialize the test data @RunWith(EasyMockRunner.class) public class MathApplicationTester { // @TestSubject annotation is used to identify class which is going to use the mock object @TestSubject MathApplication mathApplication = new MathApplication(); // @Mock annotation is used to create the mock object to be injected @Mock CalculatorService calcService; @Test public void testAdd(){ //add the behavior of calc service to add two numbers EasyMock.expect(calcService.add(10.0,20.0)).andReturn(30.00); calcService.serviceUsed(); EasyMock.expectLastCall().times(1); //activate the mock EasyMock.replay(calcService); //test the add functionality Assert.assertEquals(mathApplication.add(10.0, 20.0),30.0,0); //verify call to calcService is made or not EasyMock.verify(calcService); } }
步驟 4:執行測試用例
在 **C:\> EasyMock_WORKSPACE** 中建立一個名為 TestRunner 的 java 類檔案來執行測試用例。
檔案: TestRunner.java
import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(MathApplicationTester.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }
步驟 5:驗證結果
使用 **javac** 編譯器編譯類,如下所示:
C:\EasyMock_WORKSPACE>javac Calculator Service.java Math Application.java Math Application Tester.java Test Runner.java
現在執行 Test Runner 以檢視結果:
C:\EasyMock_WORKSPACE>java TestRunner
輸出
驗證輸出。
true
calcService.serviceUsed() 呼叫兩次的示例
步驟 1:建立一個名為 CalculatorService 的介面,提供數學函式。
檔案: CalculatorService.java
public interface CalculatorService { public double add(double input1, double input2); public double subtract(double input1, double input2); public double multiply(double input1, double input2); public double divide(double input1, double input2); public void serviceUsed(); }
步驟 2:建立一個 JAVA 類來表示 MathApplication。
檔案: MathApplication.java
public class MathApplication { private CalculatorService calcService; public void setCalculatorService(CalculatorService calcService){ this.calcService = calcService; } public double add(double input1, double input2){ calcService.serviceUsed(); calcService.serviceUsed(); return calcService.add(input1, input2); } public double subtract(double input1, double input2){ return calcService.subtract(input1, input2); } public double multiply(double input1, double input2){ return calcService.multiply(input1, input2); } public double divide(double input1, double input2){ return calcService.divide(input1, input2); } }
步驟 3:測試 MathApplication 類
讓我們透過向其中注入 calculatorService 的模擬來測試 MathApplication 類。模擬將由 EasyMock 建立。
檔案: MathApplicationTester.java
import org.easymock.EasyMock; import org.easymock.EasyMockRunner; import org.easymock.Mock; import org.easymock.TestSubject; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; // @RunWith attaches a runner with the test class to initialize the test data @RunWith(EasyMockRunner.class) public class MathApplicationTester { // @TestSubject annotation is used to identify class which is going to use the mock object @TestSubject MathApplication mathApplication = new MathApplication(); //@Mock annotation is used to create the mock object to be injected @Mock CalculatorService calcService; @Test public void testAdd(){ //add the behavior of calc service to add two numbers EasyMock.expect(calcService.add(10.0,20.0)).andReturn(30.00); calcService.serviceUsed(); EasyMock.expectLastCall().times(1); //activate the mock EasyMock.replay(calcService); //test the add functionality Assert.assertEquals(mathApplication.add(10.0, 20.0),30.0,0); //verify call to calcService is made or not EasyMock.verify(calcService); } }
步驟 4:執行測試用例
在 **C:\> EasyMock_WORKSPACE** 中建立一個名為 TestRunner 的 java 類檔案來執行測試用例。
檔案: TestRunner.java
import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(MathApplicationTester.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }
步驟 5:驗證結果
使用 **javac** 編譯器編譯類,如下所示:
C:\EasyMock_WORKSPACE>javac CalculatorService.java MathApplication.java MathApplicationTester.java TestRunner.java
現在執行 Test Runner 以檢視結果:
C:\EasyMock_WORKSPACE>java TestRunner
輸出
驗證輸出。
testAdd(com.tutorialspoint.mock.MathApplicationTester): Unexpected method call CalculatorService.serviceUsed(): CalculatorService.add(10.0, 20.0): expected: 1, actual: 0 CalculatorService.serviceUsed(): expected: 1, actual: 2 false
不呼叫 calcService.serviceUsed() 的示例
步驟 1:建立一個名為 Calculator Service 的介面,提供數學函式
檔案: CalculatorService.java
public interface CalculatorService { public double add(double input1, double input2); public double subtract(double input1, double input2); public double multiply(double input1, double input2); public double divide(double input1, double input2); public void serviceUsed(); }
步驟 2:建立一個 JAVA 類來表示 MathApplication
檔案: MathApplication.java
public class MathApplication { private CalculatorService calcService; public void setCalculatorService(CalculatorService calcService){ this.calcService = calcService; } public double add(double input1, double input2){ return calcService.add(input1, input2); } public double subtract(double input1, double input2){ return calcService.subtract(input1, input2); } public double multiply(double input1, double input2){ return calcService.multiply(input1, input2); } public double divide(double input1, double input2){ return calcService.divide(input1, input2); } }
步驟 3:測試 MathApplication 類
讓我們透過向其中注入 calculatorService 的模擬來測試 MathApplication 類。模擬將由 EasyMock 建立。
檔案: MathApplicationTester.java
import org.easymock.EasyMock; import org.easymock.EasyMockRunner; import org.easymock.Mock; import org.easymock.TestSubject; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; // @RunWith attaches a runner with the test class to initialize the test data @RunWith(EasyMockRunner.class) public class MathApplicationTester { // @TestSubject annotation is used to identify class which is going to use the mock object @TestSubject MathApplication mathApplication = new MathApplication(); //@Mock annotation is used to create the mock object to be injected @Mock CalculatorService calcService; @Test public void testAdd(){ //add the behavior of calc service to add two numbers EasyMock.expect(calcService.add(10.0,20.0)).andReturn(30.00); calcService.serviceUsed(); EasyMock.expectLastCall().times(1); //activate the mock EasyMock.replay(calcService); //test the add functionality Assert.assertEquals(mathApplication.add(10.0, 20.0),30.0,0); //verify call to calcService is made or not EasyMock.verify(calcService); } }
步驟 4:執行測試用例
在 **C:\> EasyMock_WORKSPACE** 中建立一個名為 TestRunner 的 java 類檔案來執行測試用例。
檔案: TestRunner.java
import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(MathApplicationTester.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }
步驟 5:驗證結果
使用 **javac** 編譯器編譯類,如下所示:
C:\EasyMock_WORKSPACE>javac Calculator Service.java Math Application.java Math Application Tester.java Test Runner.java
現在執行 Test Runner 以檢視結果:
C:\EasyMock_WORKSPACE>java TestRunner
輸出
驗證輸出。
testAdd(com.tutorialspoint.mock.MathApplicationTester): Expectation failure on verify: CalculatorService.serviceUsed(): expected: 1, actual: 0 false