敲碎时间的人的个人专栏
上一篇

Mockito——Resetting Mock

广告
选中文字可对指定文章内容进行评论啦,→和←可快速切换按钮,绿色背景文字可以点击查看评论额。
大纲

Mockito 提供了重置Mock的功能,以便以后可以重用。看看下面的代码片段。

//reset mock
reset(calcService);

在这里,我们重置了Mock对象。 MathApplication 使用 calcService 并在重置模拟后,使用模拟方法将失败测试。

举一个例子:

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

大家注意在这里,testAddAndSubtract测试中,第一个测试能够通过,但是第二个测试开始前,我们重置了Mock,这样第二个测试显然就不能通过啦。这里问一个问题,重置以后,这个新的计算返回的值应该是哪里的呢?

输出结果如下:

testAddAndSubtract(MathApplicationTester): expected:<0.0> but was:<30.0>
false

 

行为驱动开发是一种编写测试的风格,使用给定、何时以及格式化为测试方法。 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);

在这里,我们使用 BDDMockito 类的given方法而不是when .

举个例子:

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

使用以上写法的好处是代码给人一种见文知意的感觉。

 

Mockito 提供了一个特殊的 Timeout 选项来测试一个方法是否在规定的时间范围内被调用。

语法如下:

//passes when add() is called within 100 ms.
verify(calcService,timeout(100)).add(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 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 add method to be completed within 100 ms
      verify(calcService, timeout(100)).add(20.0,10.0);
	  
      //invocation count can be added to ensure multiplication invocations
      //can be checked within given timeframe
      verify(calcService, timeout(100).times(1)).subtract(20.0,10.0);
   }
}

这里的例子意思是加法和减法只要在100ms以内执行都算测试通过。

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