PowerMock complains about invalid arguments, even if a private method mocks

I tried PowerMock and I'm trying to mock a personal method:

expectPrivate(n, "doLogin", anyString(), anyString()).andReturn(true);

That is, I want to truereturn from doLoginregardless of the parameters passed. The public method that delegates this private method simply passes arguments. Here is the definition of the class to mock:

class N {
        public boolean login(String username, String password) {
            return doLogin(username, password);
        }
        private boolean doLogin(String u, String p){
            //validate login
            //...
            //...
            return true;
        }
     }

And this is a test class where I try to call mock:

import static org.junit.Assert.assertEquals;
import static org.powermock.api.easymock.PowerMock.createPartialMock;
import static org.powermock.api.easymock.PowerMock.expectPrivate;
import static org.powermock.api.easymock.PowerMock.replay;
import static org.powermock.api.easymock.PowerMock.verify;
import static org.mockito.Matchers.anyString;

import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

@RunWith(PowerMockRunner.class)
@PrepareForTest(N.class)
public class NodeAccessorTest {
private String username = "admin";
private String password = "asdf";

@Test
public void testMockLogin() throws Exception {
    N n = createPartialMock(N.class,
            "doLogin", String.class, String.class);
    boolean expected = true;
    expectPrivate(n, "doLogin", anyString(), anyString()).andReturn(expected);
    replay(n);
    boolean actual = n.login("A", "B");
    verify(n);
    assertEquals("Expected and actual did not match", expected, actual);
   }
}

This is the failure trace:

java.lang.AssertionError: 
  Unexpected method call N.doLogin("A", "B"):
    N.doLogin("", ""): expected: 1, actual: 0
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
    at org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl.invoke(EasyMockMethodInvocationControl.java:91)
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:124)
    at org.powermock.core.MockGateway.methodCall(MockGateway.java:185)
    at com.pugmarx.mock.N.doLogin(N.java)
    at com.pugmarx.mock.N.login(N.java:60)

Thus, the mocking structure is unhappy when concrete ones Stringare passed to the method public login(), but great when used anyString. Ideally, I would expect that since the challenge is private doLoginridiculed, this should not be so. What am I missing?

+4
source
1

, , Matchers anyString(), PowerMock expectPrivate.

Mockito Matches EasyMock Matchers: org.mockito.Matchers.anyString.

: EasyMock.anyString()

import org.easymock.EasyMock;

...

expectPrivate(n, "doLogin", EasyMock.anyString(), EasyMock.anyString()).andReturn(expected);

, .

+2

All Articles