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

      <tfoot id='UsEDP'></tfoot>

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

        <legend id='UsEDP'><style id='UsEDP'><dir id='UsEDP'><q id='UsEDP'></q></dir></style></legend>

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

        模拟 java.time.format.DateTimeFormatter 类

        时间:2023-09-26
        <i id='MQO6S'><tr id='MQO6S'><dt id='MQO6S'><q id='MQO6S'><span id='MQO6S'><b id='MQO6S'><form id='MQO6S'><ins id='MQO6S'></ins><ul id='MQO6S'></ul><sub id='MQO6S'></sub></form><legend id='MQO6S'></legend><bdo id='MQO6S'><pre id='MQO6S'><center id='MQO6S'></center></pre></bdo></b><th id='MQO6S'></th></span></q></dt></tr></i><div id='MQO6S'><tfoot id='MQO6S'></tfoot><dl id='MQO6S'><fieldset id='MQO6S'></fieldset></dl></div>

        • <bdo id='MQO6S'></bdo><ul id='MQO6S'></ul>

            <legend id='MQO6S'><style id='MQO6S'><dir id='MQO6S'><q id='MQO6S'></q></dir></style></legend>
              1. <tfoot id='MQO6S'></tfoot>
                  <tbody id='MQO6S'></tbody>

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

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

                  问题描述

                  我正在尝试模拟 DateTimeFormatter 类.我做了以下事情:

                  I'm trying to mock the DateTimeFormatter class. I've done the following:

                  @RunWith(PowerMockRunner.class)
                  @PrepareForTest({DateTimeFormatter.class})
                  public class UnitTest {
                  
                  private DateTimeFormatter mockDateFormatter;
                  
                  private AwesomeClass awesomeClass;
                  
                  @Before
                  public void setUp() {
                      mockDateFormatter = PowerMockito.mock(DateTimeFormatter.class);
                      awesomeClass = new AwesomeClass(mockDateFormatter);
                  }
                  
                  @Test
                  public void shouldToTestSomethingAwesome() {
                     // Other test code
                      PowerMockito.when(mockDateFormatter.format(any(LocalDate.class)))
                                      .thenReturn("20150224");
                     // Other test code
                  
                  }
                  

                  AwesomeClass 使用它来格式化 LocalDateTime.now(ZoneId.of("UTC"));.然后,格式化的字符串进一步用于生成另一个字符串.我需要确保正确生成字符串.所以我需要从格式化程序返回一致的日期或模拟 LocalDateTime.now(..) 静态方法

                  AwesomeClass uses it to format LocalDateTime.now(ZoneId.of("UTC"));. The formatted string is then further used to generate another string. I need to ensure that the string is properly generated. So I need to return a consistent date from either the formatter or mock the LocalDateTime.now(..) static method

                  我做错了什么?

                  推荐答案

                  模拟 LocalDateTime.now() 的替代方法是将时钟注入您的类并更改(或添加另一个)构造函数像这样:

                  An alternative to mocking LocalDateTime.now() is to inject the clock into your class and change your (or add another) constructor like this:

                  AwesomeClass(DateTimeFormatter fmt, Clock clock) {
                    //instead of LocalDateTime now = LocalDateTime.now():
                    LocalDateTime now = LocalDateTime.now(clock);
                  }
                  

                  然后在你的测试中:

                  new AwesomeClass(formatter, Clock.fixed(the time you want here));
                  

                  这篇关于模拟 java.time.format.DateTimeFormatter 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:用 Mockito 模拟重载的方法 下一篇:何时使用 Mockito.verify()?

                  相关文章

                  最新文章

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

                    <tfoot id='SLwg9'></tfoot>

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