我应该如何为该getUrl
方法编写测试?
public class UrlList {
private final String[] urls;
private int index;
private SecureRandom random;
public static enum Mode {
VALUE_1,
VALUE_2,
VALUE_3;
}
public UrlList(String... urls) {
if (urls == null || urls.length == 0) {
throw new IllegalArgumentException("The url list may bot be null or empty!");
}
this.urls = urls;
this.index = 0;
this.random = new SecureRandom();
}
public String getUrl(Mode mode) {
switch (mode) {
case VALUE_1:
return urls[0];
case VALUE_2:
return urls[random.nextInt(urls.length)];
case VALUE_3:
try {
return urls[index];
} finally {
index = (index + 1) % urls.length;
}
default:
throw new RuntimeException("Unknown mode!");
}
}
}
在上面的代码urls
中是一个字符串数组。
主要问题是我应该如何测试case VALUE_3:
?
因为第一次测试,index = 0
但之后的值index
将更改为块中的其他值,我想在同一个单元测试类finally
中用新值再次测试它。index