2014-02-13 4 views

답변

10

는 엄격한 모의 불평하지만 멋진 모의은하지 않습니다. 그것이 순서 확인의 의미입니다.

0

EasyMock.createStrictMock()은 모의 객체를 만들고 모의 작업이 수행 될 때 모의 메소드 호출 순서를 처리합니다. Click here for complete tutorial.

@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 예상 (방법) 및 여러 어설 (방법 항목)를 만들 수 있습니다 만들 수 있습니다 주장 (방법 2), ...

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