Power Mock - 模拟最终类时出现断言错误

发布于 2024-12-06 14:07:44 字数 3436 浏览 1 评论 0原文

我试图使用 Power Mock 来模拟某些场景。

我在以下测试方法中收到断言错误 - returnSevenTest()

要测试的类

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);
    }
}

最终类

package tutorial.one;

public final class DependentFinalClass {

    final int callMeToAddOne(Boolean need)
    {
        if(need.equals(true))
            return 1;
    else
            return 1;
    }
}

使用 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();
   }

}

收到错误:

java .lang.断言错误: 验证时预期失败: tutorial.one.TutorialOne(tutorial.one.DependentFinalClass$$EnhancerByCGLIB$$fa2e41a0@5a9de6):预期:1,实际:0 在 org.powermock.api.easymock.internal.inspirationcontrol.NewInitationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(NewInitationControlAssertionError.java:20) 在 org.powermock.api.easymock.PowerMock.verifyClass(PowerMock.java:2279) 在 org.powermock.api.easymock.PowerMock.verify(PowerMock.java:1646) 在 org.powermock.api.easymock.PowerMock.verifyAll(PowerMock.java:1586) 在tutorial.one.TutorialOneTests.returnSevenTest(TutorialOneTests.java:174) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(来源未知) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(来源未知) 在 java.lang.reflect.Method.invoke(Unknown Source)

非常感谢任何帮助!谢谢

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
            return 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2024-12-13 14:07:44

我认为问题在于你已经声明了期望:
expectNew(TutorialOne.class, FinalDependentTest).andReturn(privateTest);
但在你的方法调用中它没有被调用,尝试在返回后添加anyTimes():
expectNew(TutorialOne.class, FinalDependentTest).andReturn(privateTest).anyTimes();
或删除此期望,因为您在测试中不使用构造函数,而只是使用部分模拟实例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文