Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock does not take affect when mocking overload private method and call real method #964

Open
huaronghao opened this issue Dec 29, 2018 · 1 comment

Comments

@huaronghao
Copy link

When I mocking a overload private method, It does not take any affect. I found this may be effected by “Matchers.any(Class clazz)” can not passed by variable parameter, and then WhiteboxImpl.java on line 870 will always pick up last passed method.
following is mycode:

package com.ehm.cost.service.impl;

public class ParamsClass {
}
package com.ehm.cost.service.impl;

import java.util.List;

public class TestOverload {

    public String CallOverload(ParamsClass paramsClass, List<ParamsClass> s) {
        return overload(paramsClass, s);
    }

    private String overload(List a, List<ParamsClass> b) {
        return "call real overloadSecond";
    }

    private String overload(ParamsClass a, List<ParamsClass> b) {
        return "call real overloadFirst";
    }
}
package com.ehm.cost.service.impl;

import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyListOf;

@RunWith(PowerMockRunner.class)
@PrepareForTest(TestOverload.class)
public class TestOverloadTest {
    @Spy
    @InjectMocks
    private TestOverload testOverload = new TestOverload();

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testCallOverload() throws Exception {
        PowerMockito.doReturn("call mock method").when(testOverload, "overload", any(ParamsClass.class), anyListOf(ParamsClass.class));
        String returnS = testOverload.CallOverload(new ParamsClass(), Lists.newArrayList());
        System.out.println(returnS);
    }
}

When I run test,the result is:
call real overloadFirst
but expect:
call mock method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants