Mockito——创建Mock

Mock创建

到目前为止,我们已经使用注解来创建Mock。 Mockito 提供了各种方法来创建模拟对象。 mock() 创建模拟,而不用担心Mock将在适当的时候进行的方法调用的顺序。

创建语法:

calcService = mock(CalculatorService.class);

我们再来看看一个具体的例子:

// @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);

      //subtract the behavior to subtract numbers
      when(calcService.subtract(20.0,10.0)).thenReturn(10.0);

      //test the subtract functionality
      Assert.assertEquals(mathApplication.subtract(20.0, 10.0),10.0,0);

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

      //verify call to calcService is made or not
      verify(calcService).add(20.0,10.0);
      verify(calcService).subtract(20.0,10.0);
   }
}

以上代码通过在其中注入一个calculatorService 的模拟来测试MathApplication 类。 Mock 将由 Mockito 创建。 在这里,我们通过 when() 向模拟对象添加了两个模拟方法调用 add() 和减法()。然而,在测试期间,我们在调用 add() 之前调用了 subtract()。当我们使用 create() 创建模拟对象时,方法的执行顺序无关紧要。

下面我们再说说

有序验证

Mockito 提供了 Inorder 类,该类负责处理模拟将在适当的时候进行的方法调用的顺序。

语法:

//create an inOrder verifier for a single mock
InOrder inOrder = inOrder(calcService);

//following will make sure that add is first called then subtract is called.
inOrder.verify(calcService).add(20.0,10.0);
inOrder.verify(calcService).subtract(20.0,10.0);

我们还是用一个例子来说明

// @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);

      //subtract the behavior to subtract numbers
      when(calcService.subtract(20.0,10.0)).thenReturn(10.0);

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

      //test the subtract functionality
      Assert.assertEquals(mathApplication.subtract(20.0, 10.0),10.0,0);

      //create an inOrder verifier for a single mock
      InOrder inOrder = inOrder(calcService);

      //following will make sure that add is first called then subtract is called.
      inOrder.verify(calcService).subtract(20.0,10.0);
      inOrder.verify(calcService).add(20.0,10.0);

   }
}

我们通过在其中注入一个calculatorService 的模拟来测试MathApplication 类。 Mock 将由 Mockito 创建。

在这里,我们通过 when() 向模拟对象添加了两个模拟方法调用 add() 和减法()。然而,在测试期间,我们在调用 add() 之前调用了 subtract()。当我们使用 Mockito 创建模拟对象时,方法的执行顺序无关紧要。使用 InOrder 类,我们可以确保调用顺序。

注意,在这里我特意打乱了顺序,让测试不通过,这样来验证InOrder inOrder = inOrder(calcService)这个功能的作用。

版权声明:著作权归作者所有。

没想到大家现在对单元测试这么重视啦;谢谢大家的评论和鼓励,我会再接再厉的。
我也是刚刚学习Mockito,您的文章可以作为基础知识来学习;希望以后能深入高级功能
同意楼主的意见,弄个git就好啦
而且有了一个笔记目录,写得挺清晰的,就是如果弄个git工程就好了
看了您的上一篇文章感觉不错呀
哈哈,您的这些都是mock最基础的功能,不过呢?入门还是不错的
thumb_up 3 | star_outline 1 | textsms 6