我有一个名为StringProcessor. 其中的breakLongWords()方法,只要字符序列缺少预定义长度的空格,就会向输入添加零宽度空格:
public class StringProcessor {
private static final int WORD_MAX_LENGTH = 40;
public String breakLongWords(CharSequence input) {
// add a zero-width space character after a word
// if its length is greater than WORD_MAX_LENGTH and doesn't have any space in it
}
}
static 字段WORD_MAX_LENGTH是一个实现细节,不应暴露给其他类(包括测试类)。
现在,如何在不访问 JUnit 的情况下测试边缘情况WORD_MAX_LENGTH?例如:
@Test
public void breakLongWords_EdgeCase() {
String brokenText = stringProcessor.breakLongWords
("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // its length should be = WORD_MAX_LENGTH
assertEquals(41, brokenText.length()); // 41 (WORD_MAX_LENGTH + 1) is also hard-coded here
}
慕码人2483693
天涯尽头无女友
开心每一天1111
相关分类