开发者

Power Mock - Assertion error when mocking final class

开发者 https://www.devze.com 2023-04-07 18:15 出处:网络
I was trying to mock certain scenarios using Power Mock. I am getting assertion error on below test method - returnSevenTest().

I was trying to mock certain scenarios using Power Mock.

I am getting assertion error on below test method - returnSevenTest().

Class to be Tested

package tutorial.one;

    public class TutorialOne {

    //fields 

    private int number;

    private String name;

    private final DependentFinalClass finalObj;


    public TutorialOne (DependentFinalClass dep) {
        this.finalObj= dep ; 
    }

    public TutorialOne(int number, String name) {
        super();
        this.number = number;
        this.name = name;
        finalObj = new DependentFinalClass();
    }

    public static int returnTwo()
    {       
    return 2;   
    }

    public int returnThree()
    {
        return (returnTwo()+1);
    }


    public final int returnFour()
    {   
        return (returnThree()+1);
    }

    private final int returnFive(int addOne)
    {   
        return (returnFour()+addOne);
    }

    public int returnSix(int addOne)
    {
        return (returnFive(1)+addOne);
    }

    public int returnSeven()
    {
        final int addValue= finalObj.callMeToAddOne(true);
        return (returnSix(1)+ addValue);
    }
}

Final Class

package tutorial.one;

public final class DependentFinalClass {

    final int callMeToAddOne(Boolean need)
    {
        if(need.equals(true))
            return 1;
    else
            retu开发者_运维问答rn 1;
    }
}

Test performing Class using Power Mock+ Easy Mock

package tutorial.one;

import  stmts ....


@RunWith(PowerMockRunner.class)

//We prepare the IdGenerator for test because the static method is normally not
//mockable
@PrepareForTest({TutorialOne.class, DependentFinalClass.class}) public class TutorialOneTests {

    @Test
    public void returnSevenTest()  throws Exception 
    {
        int expected=7;
        TutorialOne privateTest =createPartialMock(TutorialOne.class,"returnFive" );

        DependentFinalClass finalDependentTest = createPartialMock(DependentFinalClass.class,"callMeToAddOne" );
        expect(finalDependentTest.callMeToAddOne(true)).andReturn(1);


        expectNew(TutorialOne.class, finalDependentTest).andReturn(privateTest);
        expectPrivate(privateTest, "returnFive", 1).andReturn(5);


        replayAll(TutorialOne.class,finalDependentTest,privateTest);
        int actual = privateTest.returnSeven();
        verifyAll();
        assertEquals("Expected and actual did not match", expected, actual);
        resetAll();
   }

}

Error Recieved:

java.lang.AssertionError: Expectation failure on verify: tutorial.one.TutorialOne(tutorial.one.DependentFinalClass$$EnhancerByCGLIB$$fa2e41a0@5a9de6): expected: 1, actual: 0 at org.powermock.api.easymock.internal.invocationcontrol.NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(NewInvocationControlAssertionError.java:20) at org.powermock.api.easymock.PowerMock.verifyClass(PowerMock.java:2279) at org.powermock.api.easymock.PowerMock.verify(PowerMock.java:1646) at org.powermock.api.easymock.PowerMock.verifyAll(PowerMock.java:1586) at tutorial.one.TutorialOneTests.returnSevenTest(TutorialOneTests.java:174) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)

Any help is well appreciated!! Thanks


I think the problem is that you've declared expectation : expectNew(TutorialOne.class, finalDependentTest).andReturn(privateTest); but in your method call it's not called , try to add anyTimes() after return : expectNew(TutorialOne.class, finalDependentTest).andReturn(privateTest).anyTimes(); or remove this expectation because you don't use constructor in testing you just use partial mocked instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消