【发布时间】:2014-10-31 01:09:37
【问题描述】:
我编写了以下方法,该方法从具有开始和结束索引的字符串中获取子字符串。对于会超出范围的索引,如何在 Junit 中编写测试用例?例如,如果字符串是香蕉,并且方法作为 getSubstring(3,12) 运行,则该方法将抛出越界错误。如何编写一个在显示此错误时将通过的测试用例?
public String getSubstring(int start, int end){
sub = MyString.str.substring(start, end);
return sub;
}
@Test
public void testgetSubstring() {
MyString test = new MyString();
String result = test.getSubstring(3,12);
}
【问题讨论】:
-
使用字符串长度函数
标签: java string testing indexing