2

I am trying to mock a private method of a class.

I want to mock isBalancedTree method for the below class.

public class DummyClassForPowerMockTest {

    public boolean isBalancedTree_1() {
        System.out.println("Inside isBalancedTree_1");
        return isBalancedTree("hello");
    }

    private boolean isBalancedTree(String a) {
        System.out.println("Inside the isBalancedTree");
        return false;
    }
}

The below is the test code.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:spring-configuration/unit-testing-config.xml" })
@PrepareForTest(DummyClassForPowerMockTest.class)
public class PowerMockTest {

    final String methodToTest = "isBalancedTree";

    @Resource(name = "notify")
    private DummyClassForPowerMockTest notify;

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Before
    public void initialize(){
        PowerMock.resetAll();
    }

    @Test
    public void testA() throws Exception{
        PowerMock.expectPrivate(notify, methodToTest, EasyMock.anyObject(String.class)).andReturn(true).anyTimes();

        PowerMock.replayAll();
        boolean result = notify.isBalancedTree_1();
        Assert.assertEquals(true, result);
        PowerMock.verifyAll();
    }
}

I am creating the bean for the DummyClassForPowerMockTest from spring as

public class PowerMockCreatePartialMockFactoryBean<T> implements FactoryBean<T> {

    private Class<T> classToMock;
    private String   methodToMock;

    public PowerMockCreatePartialMockFactoryBean(Class<T> classToMock, String methodName) {
        this.classToMock = classToMock;
        this.methodToMock = methodName;
        System.out.println("Inside the power mock create partial constructor .. "+methodToMock);
    }

    @Override
    public T getObject() throws Exception {
        System.out.println("Inside the getObject method ........ "+methodToMock);
        return PowerMock.createPartialMock(classToMock, methodToMock);
    }

    @Override
    public Class<T> getObjectType() {
        return classToMock;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}

with the below bean.

<bean id="notify" class="PowerMockCreatePartialMockFactoryBean">
    <constructor-arg index="0" value="DummyClassForPowerMockTest" />
    <constructor-arg index="1" value="isBalancedTree"/>
</bean>

With this I am getting the following error.

[junit] Testcase: testA took 3.24 sec
[junit]     Caused an ERROR
[junit] no last call on a mock available
[junit] java.lang.IllegalStateException: no last call on a mock available
[junit]     at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:520)
[junit]     at org.easymock.EasyMock.expectLastCall(EasyMock.java:511)
[junit]     at org.powermock.api.easymock.PowerMock.doExpectPrivate(PowerMock.java:2248)
[junit]     at org.powermock.api.easymock.PowerMock.expectPrivate(PowerMock.java:1400)
[junit]     at org.powermock.api.easymock.PowerMock.expectPrivate(PowerMock.java:1411)
[junit]     at org.powermock.api.easymock.PowerMock.expectPrivate(PowerMock.java:1357)

I am not sure why this error is coming up. I have searched a lot on web for this error, but nothing helped. When I do not initialize the class with bean, then I am able to run the test successfully. But when I try to inject a bean it fails with the above error. Can someone please help me out on this.

Thanks in advance.

Harshit Agrawal
  • 903
  • 1
  • 7
  • 11

0 Answers0