EasyMock – createMock



到目前為止,我們已使用註釋來建立模擬。EasyMock 提供多種方法來建立模擬物件。EasyMock.createMock() 建立模擬,而無需擔心模擬在執行期間將進行的方法呼叫的順序。

語法

calcService = EasyMock.createMock(CalculatorService.class);

示例

步驟 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);
}

步驟 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 建立。

在這裡,我們透過 expect() 向模擬物件添加了兩個模擬方法呼叫,add() 和 subtract()。但在測試期間,我們已在呼叫 add() 前呼叫了 subtract()。當我們使用 EasyMock.createMock() 建立模擬物件時,方法執行的順序無關緊要。

檔案:MathApplicationTester.java

import org.easymock.EasyMock;
import org.easymock.EasyMockRunner;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(EasyMockRunner.class)
public class MathApplicationTester {
   private MathApplication mathApplication;
   private CalculatorService calcService;
   
   @Before
   public void setUp(){
      mathApplication = new MathApplication();
      calcService = EasyMock.createMock(CalculatorService.class);
      mathApplication.setCalculatorService(calcService);
   }
   @Test
   public void testAddAndSubtract(){
      //add the behavior to add numbers
      EasyMock.expect(calcService.add(20.0,10.0)).andReturn(30.0);
      
      //subtract the behavior to subtract numbers
      EasyMock.expect(calcService.subtract(20.0,10.0)).andReturn(10.0);
      
      //activate the mock
      EasyMock.replay(calcService);	
	
      //test the subtract functionality
      Assert.assertEquals(mathApplication.subtract(20.0, 10.0),10.0,0);
      
      //test the add functionality
      Assert.assertEquals(mathApplication.add(20.0, 10.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 MathApplicationTester.java

現在執行測試執行器以檢視結果 −

C:\EasyMock_WORKSPACE>java TestRunner

輸出

驗證輸出。

true
廣告