When writing unit tests related to XML (for example, test a class that reads / generates XML), I used XML-String / my input XML String in separate files next to my unit test to write approved results. Say I have a class called "MyTransformer" that converts one XML format to another. Then I would create three files in one package:
- MyTransformerTest.java
- MyTransformerTestSampleInput.xml
- MyTransformerTestExpectedOutput.xml
Then my statement may look like this (simplified pseudocode for reasons of simplicity):
Reader transformed = MyTransformer.transform(getResourceAsStream("MyTransformerTestSampleInput.xml"))); Reader expected = getResourceAsStream("MyTransformerTestExpectedOutput.xml"); assertXMLEqual(expected, transformed);
However, a colleague told me that access to the file that I have in this unit test is not acceptable. He suggested creating a literal string constant (private static final String) containing my XML file content, possibly in a separate groovy class due to the advantage of multi-line strings, and not for writing an XML file to files.
I don't like the idea of ββstring string constants, because even if I have multi-line strings in groovy, I still lose the syntax highlighting and all the other useful features of my XML editor that tell me right away if my XML is syntax errors, etc. .
What do you think? Is file access really bad? If yes: why? If not, then why is this normal?
source share