Mockito - 重置模擬



Mockito 提供了重置模擬的能力,以便它可以在以後重複使用。看看以下程式碼段。

//reset mock
reset(calcService);

在此處,我們重置了模擬物件。MathApplication 使用 calcService,在重置模擬後,使用模擬方法將失敗測試。

示例

步驟 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 類

讓我們測試 MathApplication 類,透過注入 calculatorService 的模擬。模擬將由 Mockito 建立。

檔案:MathApplicationTester.java

package com.tutorialspoint.mock;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.reset;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

// @RunWith attaches a runner with the test class to initialize the test data
@RunWith(MockitoJUnitRunner.class)
public class MathApplicationTester {
	
   private MathApplication mathApplication;
   private CalculatorService calcService;

   @Before
   public void setUp(){
      mathApplication = new MathApplication();
      calcService = mock(CalculatorService.class);
      mathApplication.setCalculatorService(calcService);
   }

   @Test
   public void testAddAndSubtract(){

      //add the behavior to add numbers
      when(calcService.add(20.0,10.0)).thenReturn(30.0);
  
      //test the add functionality
      Assert.assertEquals(mathApplication.add(20.0, 10.0),30.0,0);

      //reset the mock	  
      reset(calcService);

      //test the add functionality after resetting the mock
      Assert.assertEquals(mathApplication.add(20.0, 10.0),30.0,0);   
   }
}

步驟 4 - 執行測試用例

C:\> Mockito_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:\Mockito_WORKSPACE>javac CalculatorService.java MathApplication.
   java MathApplicationTester.java TestRunner.java

現在執行測試執行器檢視結果 -

C:\Mockito_WORKSPACE>java TestRunner

驗證輸出。

testAddAndSubtract(MathApplicationTester): expected:<0.0> but was:<30.0>
false
廣告
© . All rights reserved.