1. <legend id='HKlqf'><style id='HKlqf'><dir id='HKlqf'><q id='HKlqf'></q></dir></style></legend>
      <i id='HKlqf'><tr id='HKlqf'><dt id='HKlqf'><q id='HKlqf'><span id='HKlqf'><b id='HKlqf'><form id='HKlqf'><ins id='HKlqf'></ins><ul id='HKlqf'></ul><sub id='HKlqf'></sub></form><legend id='HKlqf'></legend><bdo id='HKlqf'><pre id='HKlqf'><center id='HKlqf'></center></pre></bdo></b><th id='HKlqf'></th></span></q></dt></tr></i><div id='HKlqf'><tfoot id='HKlqf'></tfoot><dl id='HKlqf'><fieldset id='HKlqf'></fieldset></dl></div>

      <small id='HKlqf'></small><noframes id='HKlqf'>

        <bdo id='HKlqf'></bdo><ul id='HKlqf'></ul>

    2. <tfoot id='HKlqf'></tfoot>

      使用 Mockito 模拟 Joda DateTime 方法

      时间:2023-09-25
    3. <legend id='3mTwi'><style id='3mTwi'><dir id='3mTwi'><q id='3mTwi'></q></dir></style></legend>
    4. <small id='3mTwi'></small><noframes id='3mTwi'>

          <tbody id='3mTwi'></tbody>
          <tfoot id='3mTwi'></tfoot>
          <i id='3mTwi'><tr id='3mTwi'><dt id='3mTwi'><q id='3mTwi'><span id='3mTwi'><b id='3mTwi'><form id='3mTwi'><ins id='3mTwi'></ins><ul id='3mTwi'></ul><sub id='3mTwi'></sub></form><legend id='3mTwi'></legend><bdo id='3mTwi'><pre id='3mTwi'><center id='3mTwi'></center></pre></bdo></b><th id='3mTwi'></th></span></q></dt></tr></i><div id='3mTwi'><tfoot id='3mTwi'></tfoot><dl id='3mTwi'><fieldset id='3mTwi'></fieldset></dl></div>

            • <bdo id='3mTwi'></bdo><ul id='3mTwi'></ul>

                本文介绍了使用 Mockito 模拟 Joda DateTime 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我希望 millis 返回指定的值.

                I want millis to return specified value.

                public long myMethod(){
                    DateTime nowDateTime = new DateTime(DateTimeZone.UTC);
                    long millis = nowDateTime.getMillis();
                    System.out.println(millis);
                }
                

                我试过了,但没有运气.

                I tried this with no luck.

                @RunWith(PowerMockRunner.class)
                @PrepareForTest({ DateTime.class })
                @PowerMockIgnore({ "javax.crypto.*", "javax.management*" })
                ...
                ...
                public void testMyMethod(){
                    DateTime nowDateTime = PowerMockito.mock(DateTime.class);
                    Mockito.when(nowDateTime.getMillis()).thenReturn(10L);
                }
                

                我该如何解决这个问题?

                How can I fix this?

                推荐答案

                只需使用 JodaTime 的 org.joda.time.DateTimeUtils#setCurrentMillisFixed 方法,该方法旨在修复 new DateTime() 到与当前时间不同的时间.要返回正常时间,请稍后使用 org.joda.time.DateTimeUtils#setCurrentMillisSystem.无需嘲笑.

                Just use the org.joda.time.DateTimeUtils#setCurrentMillisFixed method of JodaTime which was designed to fix new DateTime() to a different time than the current time. To return to the normal time use org.joda.time.DateTimeUtils#setCurrentMillisSystem afterwards. No mocking needed.

                
                @Test
                public void test() {
                DateTimeUtils.setCurrentMillisFixed(10L);
                // .. your code
                }
                
                @After
                public void cleanup() {
                // Make sure to cleanup afterwards
                DateTimeUtils.setCurrentMillisSystem()
                }
                
                

                这篇关于使用 Mockito 模拟 Joda DateTime 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 Mockito 测试 Java 的行为增强 下一篇:验证是否调用了所有 getter 方法

                相关文章

                最新文章

              • <tfoot id='u1gii'></tfoot>
                <legend id='u1gii'><style id='u1gii'><dir id='u1gii'><q id='u1gii'></q></dir></style></legend>

                    <bdo id='u1gii'></bdo><ul id='u1gii'></ul>
                1. <small id='u1gii'></small><noframes id='u1gii'>

                  <i id='u1gii'><tr id='u1gii'><dt id='u1gii'><q id='u1gii'><span id='u1gii'><b id='u1gii'><form id='u1gii'><ins id='u1gii'></ins><ul id='u1gii'></ul><sub id='u1gii'></sub></form><legend id='u1gii'></legend><bdo id='u1gii'><pre id='u1gii'><center id='u1gii'></center></pre></bdo></b><th id='u1gii'></th></span></q></dt></tr></i><div id='u1gii'><tfoot id='u1gii'></tfoot><dl id='u1gii'><fieldset id='u1gii'></fieldset></dl></div>