I have a utility class called StringProcessor
. The breakLongWords()
method in it, adds zero-width spaces to the input whenever a sequence of characters lack white space for a predefined length:
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
}
}
The static field WORD_MAX_LENGTH
is an implementation detail and should not be exposed to other classes (including test classes).
Now, how can I test the edge case in JUnit without accessing WORD_MAX_LENGTH
? For example:
@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
}