我是mockito的新手。我的问题是我怎样才能使用Mockito来模拟一个for循环?如何使用Mockito模拟For循环
对于EG:这是主要类别:
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;
}
}
}
这是测试类:
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 --
}
}
我看到这个related question。但我无法理解,for循环如何被嘲弄。
明确解释这是否有帮助:http://stackoverflow.com/questions/6379308/testing-java-enhanced-for-behavior-with-mockito –
我想你需要模拟迭代器。这里是[ref](http://stackoverflow.com/questions/6379308/testing-java-enhanced-for-behavior-with-mockito) – chenrui
@Matthew真棒!谢谢 !! – Vikram