JMockit 和引用传递。我们当中一定有一个人是错的(很可能就是我!)

发布于 2024-11-28 20:21:08 字数 1131 浏览 0 评论 0原文

如果有人能帮助我解决我最近一直在努力解决的这个问题,我将不胜感激。我正在尝试模拟一个方法,该方法采用 double 和 Calendar,并返回由 Calendar 的值确定的整数。模拟定义如下:

new NonStrictExpectations()
    {
        // this is my class to be mocked
        Calculator growthCalc;
        {
            GregorianCalendar month = new GregorianCalendar(2010, 0, 1);

            for (int i = 0; i < 3; i++)
            {
                // mock the "applyInflation" method
                growthCalc.applyInflation(anyDouble, month);
                result = i;

                month = (GregorianCalendar) month.clone();  //AAA
                month.add(Calendar.MONTH, 1);
            }

            growthCalc.toString();
            result = "Mocked Calculator";
        }
    };

模拟已设置(调用 toString() 返回正确的文本),但 applyInflation 方法对除上述循环的最后一次迭代之外的所有内容返回零(即,当您传入时返回 2) new GregorianCalendar(2010,2,1) 的月份

我得出的结论是,标记为 AAA 的行没有达到我认为应有的效果,它似乎没有更改指向“月份”的新副本的指针。 “ 那我可以在不影响模拟期望设置的“月份”内容的情况下进行修改,或者 JMockit 没有按照我的想法记录模拟,

请告诉我哪里出错了。我真的很想知道这里发生了什么,因为我对 Java 的理解从根本上来说是有缺陷的,或者 JMockit 实现没有按照它“应该”的方式运行(根据我的说法:))。或者我犯了一个真正的小学生错误,开始的两个晚上都没有发现......

谢谢。

I would be very grateful if someone could help me break through this issue I've been battling with recently. I am trying to mock a method that takes a double and a Calendar, and returns an integer that is determined by the value of the Calendar. The mock definition is below:

new NonStrictExpectations()
    {
        // this is my class to be mocked
        Calculator growthCalc;
        {
            GregorianCalendar month = new GregorianCalendar(2010, 0, 1);

            for (int i = 0; i < 3; i++)
            {
                // mock the "applyInflation" method
                growthCalc.applyInflation(anyDouble, month);
                result = i;

                month = (GregorianCalendar) month.clone();  //AAA
                month.add(Calendar.MONTH, 1);
            }

            growthCalc.toString();
            result = "Mocked Calculator";
        }
    };

The mock is set-up (calling toString() returns the correct text), but the applyInflation method returns zero for everything other than the last iteration of the loop above (i.e. it returns 2 when you pass-in the month new GregorianCalendar(2010,2,1).

I have concluded that the line marked AAA isn't having the effect I think it should. It doesn't seem to be changing the pointer to point to a new copy of "month" that I can modify without impacting the contents of the "month" that the mock expectation has set. Either that, or JMockit isn't recording the mock the way I thought.

Please help! Please tell me where I'm going wrong. I would really like to know what's up here as either my understanding of Java is fundamentally flawed or the JMockit implementation doesn't behave the way it "should" (according to me :) ). Or I've made a real schoolboy error that two evenings of starting haven't spotted...

Thank you.

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

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

发布评论

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

评论(1

泪是无色的血 2024-12-05 20:21:09

你的测试完全正确。它失败只是因为 JMockit 中的一个错误,我正在立即修复该错误。抱歉给您带来麻烦了!

Your test is perfectly correct. It only fails because of a bug in JMockit, which I am fixing right away. Sorry for the trouble!

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