Mockito-adding-behavior

提供:Dev Guides
移動先:案内検索

Mockito-動作の追加

Mockitoは、* when()*メソッドを使用して、モックオブジェクトに機能を追加します。 次のコードスニペットをご覧ください。

//add the behavior of calc service to add two numbers
when(calcService.add(10.0,20.0)).thenReturn(30.00);

ここでは、Mockitoに、 calcServiceadd メソッドに10と20を追加する動作を与え、その結果、30.00の値を返すように指示しました。

この時点で、モックは動作を記録し、動作するモックオブジェクトです。

//add the behavior of calc service to add two numbers
when(calcService.add(10.0,20.0)).thenReturn(30.00);

ステップ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-MathApplicationを表すJAVAクラスを作成します

*_ファイル: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クラスをテストしましょう。 MockはMockitoによって作成されます。

*_ファイル:MathApplicationTester.java_*
import static org.mockito.Mockito.when;

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

//@RunWith attaches a runner with the test class to initialize the test data
@RunWith(MockitoJUnitRunner.class)
public class MathApplicationTester {

  //@InjectMocks annotation is used to create and inject the mock object
   @InjectMocks
   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
      when(calcService.add(10.0,20.0)).thenReturn(30.00);

     //test the add functionality
      Assert.assertEquals(mathApplication.add(10.0, 20.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

出力を確認します。

true