Difference between EasyMock.createStrictMock (class <T> x) and EasyMock.createNiceMock (class <T> x)

In the API document, doc mentions that when checking the line control order, it is turned on by default, but in the case of a nice layout - not. I did not understand what they mean by "order verification".

+4
source share
2 answers

If you tell the layout to wait for the call foo(), then wait for the call bar(), and the actual calls bar(), and then foo(), a strict layout will complain, but the nice layout won "t. That means order verification.

+10
source

EasyMock.createStrictMock() , , . : .

@Before
   public void setUp(){
      mathApplication = new MathApplication();
      calcService = EasyMock.createStrictMock(CalculatorService.class);
      mathApplication.setCalculatorService(calcService);
   }

   @Test
   public void testAddAndSubtract(){

      //add the behavior to add numbers
      EasyMock.expect(calcService.add(20.0,10.0)).andReturn(30.0);

      //subtract the behavior to subtract numbers
      EasyMock.expect(calcService.subtract(20.0,10.0)).andReturn(10.0);

      //activate the mock
      EasyMock.replay(calcService); 

      //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
      EasyMock.verify(calcService);
   }

EasyMock.createNiceMock(): , NiceMock 1 () assert (method1), assert (method2),...

@Before
   public void setUp(){
      mathApplication = new MathApplication();
      calcService = EasyMock.createNiceMock(CalculatorService.class);
      mathApplication.setCalculatorService(calcService);
   }

   @Test
   public void testCalcService(){

      //add the behavior to add numbers
      EasyMock.expect(calcService.add(20.0,10.0)).andReturn(30.0);

      //activate the mock
      EasyMock.replay(calcService); 

      //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),0.0,0);

      //test the multiply functionality
      Assert.assertEquals(mathApplication.divide(20.0, 10.0),0.0,0);        

      //test the divide functionality
      Assert.assertEquals(mathApplication.multiply(20.0, 10.0),0.0,0);

      //verify call to calcService is made or not
      EasyMock.verify(calcService);
   }
0

Source: https://habr.com/ru/post/1526678/


All Articles