tiebin-zhang / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

Mocking Private Methods #457

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I have written a code that mocks private method using Power Mockito.

Source class :This is the class that needs to be tested.
The Private Method is doTheGamble

package com.kronos.wfc.platform.messaging.framework;

import java.util.Random;

public class Abc{

    public void meaningfulPublicApi() {
        if (doTheGamble()) {
            throw new RuntimeException("boom");
        }
    }

    private boolean doTheGamble() {
        Random random = new Random(System.nanoTime());
        boolean gamble = random.nextBoolean();
        return gamble;
    }
}

The Test I wrote is

package com.kronos.wfc.platform.messaging.framework;

//import static org.mockito.Matchers.anyInt;
//import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import junit.framework.TestCase;

import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)

@PrepareForTest(Abc.class)
public class AbcMicrotest extends TestCase {

    protected void setUp() throws Exception {
        super.setUp();
        PowerMockito.mockStatic(Abc.class);
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }
    public void testmeaningfultest1()
    {
            Abc spy = PowerMockito.spy(new Abc());
try{
            PowerMockito.doReturn(true).when(spy, "doTheGamble");

            spy.meaningfulPublicApi();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }

}

Errors :

org.mockito.exceptions.base.MockitoException: 
'meaningfulPublicApi' is a *void method* and it *cannot* be stubbed with a 
*return value*!
Voids are usually stubbed with Throwables:
    doThrow(exception).when(mock).someVoidMethod();
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. The method you are trying to stub is *overloaded*. Make sure you are calling 
the right overloaded version.
2. Somewhere in your test you are stubbing *final methods*. Sorry, Mockito does 
not verify/stub final methods.
3. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub 
spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

    at com.kronos.wfc.platform.messaging.framework.AbcMicrotest.testmeaningfultest1(AbcMicrotest.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:131)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

What version of the product are you using? On what operating system?
I am working on Eclipse IDE

Please help me with this.
Thanks in advance

Original issue reported on code.google.com by sachinkh...@gmail.com on 9 Sep 2013 at 4:58