I’ve got a question about testing methods working on strings. Everytime, I write a new test on a method that has a string as a parameter.
Now, some issues come up:
- How to include a test string with \n, \r, \t, umlauts etc?
- How to set the encoding?
- Should I use external files that are opened by a FileInputStream? (too much overhead, imho)
So… what are your approaches to solve this?
Um… just type it the way you want? You can use \n, \r and \t, umlauts stc. in Java String literals; if you’re worried about the encoding of the source code file, you can use Unicode escape sequences, and you can produce them using the native2ascii tool that comes with the JDK.
Once you have a Java String, it’s too late to worry about encodings – they use UTF-16, and any encoding problems occur when translating between Strings and byte arrays (unlike C, Java keeps these concepts clearly separate)
Edit: If your Strings are too big to be comfortably used in source code or you’re really worried about the treatment of line breaks and white space, then keeping each String in a separate file is probably best; in that case, the encoding must be specified when reading the file (In the constructor of
InputStreamReader)