How to use mockito to simulate a for loop

I'm new to mockito My question is how to use mockito to simulate a for loop?

For eg: This is the main class:

import java.util.HashSet;
import java.util.Set;

    public class stringConcatination {

        public static void main(String[] args) {
            Set<String> stringSet = new HashSet();
            stringSet.add("Robert");
            stringSet.add("Jim");
            for (String s:stringSet) {
                s = "hi " + s;
            }
        }

}

This is a test class:

import java.util.HashSet;
import java.util.Set;

import org.junit.Test;
import static org.mockito.Mockito.mock;

public class stringConcatinationTest {

    @Test
    public void testMain() {
        Set mockSet = mock(HashSet.class);
        // --  How to mock For Loop --
    }

}

I saw this related question But I can't understand how the for loop is ridiculed

Solution

Since the for loop is just the syntax sugar of the iterator () loop, you can just stub the method and return the simulated iterator instance

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>