EasyMock JUnit整合


在本章中,我們將整合JUnit和EasyMock在一起。對於JUnit,請參閱JUnit教學。我們使用計算器服務的例子。目的是建立一個數學應用,它使用CalculatorService做加,減,除運算操作。我們將使用EasyMock來模擬虛擬實現CalculatorService。此外,使用註解廣泛展示註解支援JUnit和EasyMock。

以下是所採取的步驟。

建立一個介面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);
}

建立一個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);
   }
}

讓我們來測試MathApplication類,通過它注入CalculatorService作一個模擬。Mock將由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);

      //activate the mock
      EasyMock.replay(calcService);	
		
      //test the add functionality
      Assert.assertEquals(mathApplication.add(10.0, 20.0),30.0,0);
   }
}

建立一個Java類在檔案夾 C:\ > EasyMock_WORKSPACE 執行測試用例

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());
   }
}  	

驗證結果

編譯使用javac編譯如下的類

C:\EasyMock_WORKSPACE>javac CalculatorService.java MathApplication.java MathApplicationTester.java TestRunner.java

現在執行測試執行看結果

C:\EasyMock_WORKSPACE>java TestRunner

驗證輸出

true