1. <small id='kKpS6'></small><noframes id='kKpS6'>

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

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

      1. <tfoot id='kKpS6'></tfoot>
          <bdo id='kKpS6'></bdo><ul id='kKpS6'></ul>

        使用 Mockito 测试 Java 的行为增强

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

          <tbody id='R2DvY'></tbody>

            1. <small id='R2DvY'></small><noframes id='R2DvY'>

              • <bdo id='R2DvY'></bdo><ul id='R2DvY'></ul>
                <legend id='R2DvY'><style id='R2DvY'><dir id='R2DvY'><q id='R2DvY'></q></dir></style></legend>
                  <tfoot id='R2DvY'></tfoot>
                  本文介绍了使用 Mockito 测试 Java 的行为增强的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想测试一个使用 Mockito 对其进行了增强的 java 方法.问题是当我不知道如何为增强的工作设定期望时.以下代码来自 mockito google 组中未回答的问题:

                  I want to test a java method that has an enhanced for on it using Mockito. The problem is that when I don't know how to set the expectations for the enhanced for to work. The following code was gotten from an unanswered question in the mockito google group:

                  import static org.mockito.Mockito.when;
                  import static org.testng.Assert.assertTrue;
                  
                  import java.util.ArrayList;
                  import java.util.Iterator;
                  import java.util.List;
                  
                  import org.mockito.Mockito;
                  import org.testng.annotations.Test;
                  
                  public class ListTest
                  {
                  
                    @Test
                    public void test()
                    {
                      List<String> mockList = Mockito.mock(List.class);
                      Iterator<String> mockIterator = Mockito.mock(Iterator.class);
                  
                      when(mockList.iterator()).thenReturn(mockIter);
                      when(mockIter.hasNext()).thenReturn(true).thenReturn(false);
                      when(mockIter.next()).thenReturn("A");
                  
                      boolean flag = false;
                      for(String s : mockList) {
                          flag = true;
                      }
                      assertTrue(flag);
                    }
                  } 
                  

                  for 循环内的代码永远不会被执行.为迭代器设置期望不起作用,因为 java 增强的 for 内部不使用列表迭代器.设置对 List.get() 方法的期望也没有,因为增强的实现似乎也没有调用列表的 get() 方法.

                  The code inside the for loop never gets executed. Setting expectations for an iterator doesn't work, because the java enhanced for doesn't use the list iterator internally. Setting expectations for List.get() method doesn't either since the enhanced for implementation doesn't seem to call the get() method of the list either.

                  任何帮助将不胜感激.

                  推荐答案

                  模拟迭代器对我有用.请参阅下面的代码示例:

                  Mocking the iterator works for me. See below code sample:

                  import static org.junit.Assert.assertEquals;
                  import static org.mockito.Mockito.mock;
                  import static org.mockito.Mockito.when;
                  
                  import java.util.Collection;
                  import java.util.Iterator;
                  
                  import org.junit.Before;
                  import org.junit.Test;
                  
                  public class TestMockedIterator {
                  
                      private Collection<String> fruits;
                      private Iterator<String> fruitIterator;
                  
                      @SuppressWarnings("unchecked")
                      @Before
                      public void setUp() {
                          fruitIterator = mock(Iterator.class);
                          when(fruitIterator.hasNext()).thenReturn(true, true, true, false);
                              when(fruitIterator.next()).thenReturn("Apple")
                              .thenReturn("Banana").thenReturn("Pear");
                  
                          fruits = mock(Collection.class);
                          when(fruits.iterator()).thenReturn(fruitIterator);
                      }
                  
                      @Test
                      public void test() {
                          int iterations = 0;
                          for (String fruit : fruits) {
                              iterations++;
                          }
                          assertEquals(3, iterations);
                      }
                  }
                  

                  这篇关于使用 Mockito 测试 Java 的行为增强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 JUnit @Rule 使用 Mockito 进行参数化测试? 下一篇:使用 Mockito 模拟 Joda DateTime 方法

                  相关文章

                  最新文章

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

                      <bdo id='pN6gc'></bdo><ul id='pN6gc'></ul>
                    <legend id='pN6gc'><style id='pN6gc'><dir id='pN6gc'><q id='pN6gc'></q></dir></style></legend><tfoot id='pN6gc'></tfoot>