Method Call Account Assertion

I just started playing with PowerMock and EasyMock and I'm a little confused about how the method counts are calculated.

Sample code:

class ClassToBeTested{
 private boolean toBeMocked(Integer i){
  return i%2==1; 
  }
 }

      

And the test code:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassToBeTested.class)
public class ClassToBeTestedTest{

 private final Integer i=2;
 ClassToBeTested underTest;

 @Before
 public void setUp() throws Exception {
  underTest=PowerMock.createPartialMock(ClassToBeTested.class,
                    "toBeMocked");
  PowerMock.expectPrivate(underTest, "toBeMocked", i)
            .andReturn(true);
  PowerMock.replay(underTest);
 }
 @Test
 public dummyTest(){
  Assert.assertTrue(underTest.toBeMocked(i);
  //the call is: underTest.toBeMocked(2)
  //so the computation is return 2%2==1; google says it 0 :)
  //thus real method would return 0==1 (false)
  //mocked one will return true, assertion should pass, that ok
  //RERUN ASSERTION
  Assert.assertTrue(underTest.toBeMocked(i);
  //method invocation count should be now 2
}

  @After
  public void tearDown() {
   PowerMock.verify(underTest);
   //WILL FAIL with exception saying
   //the mocked method has not been called at all
   //expected value (obvious) is one
}

      

And my question is, why does the wait for the mocked method call fail? Why ClassUnderTest

does the check show that the mocked method was not called at all?

+3


source to share


1 answer


It works for me by changing the wait line to:

 PowerMock.expectPrivate(underTest, "toBeMocked", i).andReturn(true).times(2);

      

Even without this change, he does not say that his mother was mocking. It says:



  Unexpected method call toBeMocked(2):
    toBeMocked(2): expected: 1, actual: 2

      

Are you using the latest versions of PowerMock and EasyMock?

+1


source







All Articles