English 中文(简体)
Mockito - Expecting Calls
  • 时间:2024-03-23 02:04:22

Mockito - Expecting Calls


Previous Page Next Page  

Mockito就特定方法可以发出的呼吁数量进行特别检查。 Suppose MathApppcation should refer the raporservice.serviceUsed() approach only one, it should not be found raporservice. 服务单位不止一次。

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

//pmit the method call to 1, no less and no more calls are allowed
verify(calcService, times(1)).add(10.0, 20.0);

创建计算器 服务接口如下。

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

Example

Step 1 − Create an interface called CalculatorService to provide mathematical functions

<>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

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.never;

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 initiapze the test data
@RunWith(MockitoJUnitRunner.class)
pubpc class MathApppcationTester {
	
   //@InjectMocks annotation is used to create and inject the mock object
   @InjectMocks 
   MathApppcation mathApppcation = new MathApppcation();

   //@Mock annotation is used to create the mock object to be injected
   @Mock
   CalculatorService calcService;

   @Test
   pubpc void testAdd(){
      //add the behavior of calc service to add two numbers
      when(calcService.add(10.0,20.0)).thenReturn(30.00);
		
      //add the behavior of calc service to subtract two numbers
      when(calcService.subtract(20.0,10.0)).thenReturn(10.00);
      
      //test the add functionapty
      Assert.assertEquals(mathApppcation.add(10.0, 20.0),30.0,0);
      Assert.assertEquals(mathApppcation.add(10.0, 20.0),30.0,0);
      Assert.assertEquals(mathApppcation.add(10.0, 20.0),30.0,0);
      
      //test the subtract functionapty
      Assert.assertEquals(mathApppcation.subtract(20.0, 10.0),10.0,0.0);
      
      //default call count is 1 
      verify(calcService).subtract(20.0, 10.0);
      
      //check if add function is called three times
      verify(calcService, times(3)).add(10.0, 20.0);
      
      //verify that method was never called on a mock
      verify(calcService, never()).multiply(10.0,20.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