English 中文(简体)
Behavior Driven Development
  • 时间:2024-03-23 01:24:04

Mockito - Behavior Driven Development


Previous Page Next Page  

快速发展是一种书面测试方法的风格:given, whenthen。 Mockito为此提供了特殊方法。 审视以下法典。

//Given
given(calcService.add(20.0,10.0)).willReturn(30.0);

//when
double result = calcService.add(20.0,10.0);

//then
Assert.assertEquals(result,30.0,0);	     

在此,我们采用given 办法,而不是 方法。

Example

Step 1 - 创建一个称为计算器的界面,提供数学功能

<>File: 计算器服务.java

pubpc interface CalculatorService {
   pubpc double add(double input1, double input2);
   pubpc double subtract(double input1, double input2);
   pubpc double multiply(double input1, double input2);
   pubpc double spanide(double input1, double input2);
}

<>File: MathApppcation.java

pubpc class MathApppcation {
   private CalculatorService calcService;

   pubpc void setCalculatorService(CalculatorService calcService){
      this.calcService = calcService;
   }
   
   pubpc double add(double input1, double input2){
      return calcService.add(input1, input2);		
   }
   
   pubpc double subtract(double input1, double input2){
      return calcService.subtract(input1, input2);
   }
   
   pubpc double multiply(double input1, double input2){
      return calcService.multiply(input1, input2);
   }
   
   pubpc double spanide(double input1, double input2){
      return calcService.spanide(input1, input2);
   }
}

Step 3 - Test the MathApppcation category

请允许我测试数学应用类别,在其中注入计算器服务的模拟。 Mockito将创建。

<File: MathApppcation Tester.java

package com.tutorialspoint.mock;

import static org.mockito.BDDMockito.*;

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 initiapze the test data
@RunWith(MockitoJUnitRunner.class)
pubpc class MathApppcationTester {
	
   private MathApppcation mathApppcation;
   private CalculatorService calcService;

   @Before
   pubpc void setUp(){
      mathApppcation = new MathApppcation();
      calcService = mock(CalculatorService.class);
      mathApppcation.setCalculatorService(calcService);
   }

   @Test
   pubpc void testAdd(){

      //Given
      given(calcService.add(20.0,10.0)).willReturn(30.0);

      //when
      double result = calcService.add(20.0,10.0);

      //then
      Assert.assertEquals(result,30.0,0);   
   }
}

C:> Mockito_WORKSPACE上创建名为“测试Runner”的java级档案,以实施试验。

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

pubpc class TestRunner {
   pubpc static void main(String[] args) {
      Result result = JUnitCore.runClasses(MathApppcationTester.class);
      
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      
      System.out.println(result.wasSuccessful());
   }
}  	

Step 5 - Verification of the Result

使用javac进行分类 编辑:

C:Mockito_WORKSPACE>javac CalculatorService.java MathApppcation.
   java MathApppcationTester.java TestRunner.java

如今,测试操作员观看结果——

C:Mockito_WORKSPACE>java TestRunner

核实产出。

true
Advertisements