vijayjcp / powermock

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

Stubbing of super.doX() where method name is equal does not work. #509

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi everyone, I struggle with following problem:

# What steps will reproduce the problem?
 - Concrete classes:
1. Create a parent class and a inherting child class
2. create a method in parent and override it in child
3. call super.thatMethod() in child

- Test class:
1. stub parent class method to return a mocked value
2. call on child class the method and save return value
3. look if it got mocked.

# What is the expected output? What do you see instead?
 - My Test runs red but should go green.

# What version of the product are you using? On what operating system?
 - powermock-api-mockito-1.5.2.jar

# Please provide any additional information below.
These extracts should make it more clear. You can switch between X and Y and 
will see that the test will go from green to red as the mocked value won't be 
considered anymore.
But that is exactly what I need, I want to stub a simple super delegation call 
to the parent class.

PS: Complete code attached, but below code should be enough.

================= Playground =================
public class Playground extends SuperPlayground {

    @Override
    public int getNumberX() {
        // final int number = super.getNumberX();
        final int number = super.getNumberY(); // Y works, X not.
        return number + 10;
    }
}

================= SuperPlayground =================
public class SuperPlayground {

    public int getNumberX() {
        return 30;
    }

    public int getNumberY() {
        return 30;
    }
}

================= PlaygroundTest =================
@RunWith(PowerMockRunner.class)
@PrepareForTest({ SuperPlayground.class, TreeElementUtils.class })
public class PlaygroundTest {

    @InjectMocks
    Playground spieleWiese;

    @Test
    public void testSupercallStubbing() {
        // prepare
        // final Method method = MemberMatcher.method(SuperPlayground.class, "getNumberX");
        final Method method = MemberMatcher.method(SuperPlayground.class, "getNumberY");
        MemberModifier.stub(method).toReturn(90);

        // act
        final int gottenNumber = spieleWiese.getNumberX();

        // assert
        Assert.assertThat(gottenNumber, org.hamcrest.Matchers.is(100));
    }
}

Original issue reported on code.google.com by xmarcus89@gmail.com on 31 Jul 2014 at 10:57

Attachments:

GoogleCodeExporter commented 8 years ago
Did any1 look at this already? No statements? :(

Original comment by xmarcus89@gmail.com on 13 Feb 2015 at 9:29

GoogleCodeExporter commented 8 years ago
Could I please get some feedback? It's open for like 3/4 year now without even 
a response :/

Original comment by xmarcus89@gmail.com on 27 Mar 2015 at 12:43