Unit test an edge case with Junit

Mahozad :

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
}
udalmik :

Usually such fields are made "package-private" (that is with no access modifier) and unit tests are placed on the same package.
So constants are visible for tests and classes from the same package and hidden for others.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=76093&siteId=1