To compare multiline text results in JUnit Tests I often need to go from a text representation to Java code that initialize a string with the multiline text.
E.g. if the test should check for an xml string containing:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Customer>
<id>100</id>
<name>John Doe</name>
<orders>
<Order>
<address>100 main street, smalltown, pa</address>
<orderid>1100</orderid>
</Order>
<Order>
<address>5 broadway, ny, ny</address>
<orderid>1200</orderid>
</Order>
</orders>
</Customer>
I'd like to use a tool/generator that takes the above input and get the following result:
String expected ="";
expected+="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
expected+="<Customer>\n";
expected+=" <id>100</id>\n";
expected+=" <name>John Doe</name>\n";
expected+=" <orders>\n";
expected+=" <Order>\n";
expected+=" <address>100 main street, smalltown, pa</address>\n";
expected+=" <orderid>1100</orderid>\n";
expected+=" </Order>\n";
expected+=" <Order>\n";
expected+=" <address>5 broadway, ny, ny</address>\n";
expected+=" <orderid>1200</orderid>\n";
expected+=" </Order>\n";
expected+=" </orders>\n";
expected+="</Customer>\n";
and/or
// Create test file
java.io.PrintWriter srcWriter = new java.io.PrintWriter(new java.io.FileOutputStream(testFile));
srcWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
srcWriter.println("<Customer>\n");
srcWriter.println(" <id>100</id>\n");
srcWriter.println(" <name>John Doe</name>\n");
srcWriter.println(" <orders>\n");
srcWriter.println(" <Order>\n");
srcWriter.println(" <address>100 main street, smalltown, pa</address>\n");
srcWriter.println(" <orderid>1100</orderid>\n");
srcWriter.println(" </Order>\n");
srcWriter.println(" <Order>\n");
srcWriter.println(" <address>5 broadway, ny, ny</address>\n");
srcWriter.println(" <orderid>1200</orderid>\n");
srcWriter.println(" </Order>\n");
srcWriter.println(" </orders>\n");
srcWriter.println("</Customer>\n");
srcWriter.close();
// PrintWriter never throws Exceptions, one must check the error state manually
//
if (srcWriter.checkError())
{
throw new IOException( "can not write " + testFile );
}
What would be a development tool / eclipse utility or plugin to achieve this?
The output file (if any) should not be shipped with the compile result. In the file mode an equivalent for the input file should be recreated from the string literals in the java code.
Under the package demo. tests, we have created a JUnit test class file and have included a method test_JUnit () that verifies if the str1 variable and string passed in the condition are both equal. The comparison of the expected condition has been performed by the assertEquals () method which is a JUnit specific method.
1 The Console shows as below where a message reads as ‘This is the test case in this class’. 2 The JUnit result tab displays mainly the number of test cases run, number of errors and number of failures encountered i.e. ... 3 The time taken to finish the execution of the tests. 4 Displays a green bar if all the test cases are passed. More items...
The test is supposed to Fail on executing the updated code snippet as both the expected and the actual string values do not match. In the screenshot below, you can see the updated code as well as the resultant tab. On executing the JUnit class, the console and JUnit result tab shows up the below.
=> Take A Look At The JUnit Beginners Guide Here. Let’s begin creating the JUnit test in Eclipse. #2) Create a Project folder through the navigation flow: File->New->Java Project. Another window opens up where the user needs to enter the Project folder name. The screenshot is given below.
Assuming you are copying the multi-line text from another source, and you are using Eclipse, it can automatically convert your text into a multiline String literal.
In my version, enable it under Windows -> Preferences -> Java -> Editor -> Typing -> Escape text when pasting into a String literal
Then if you type
String expected = "
and copy some text like
blah
blah
blah
blah
blah
and then paste your string, Eclipse creates:
String expected = "blah\n" +
"blah\n" +
"blah\n" +
"blah\n" +
"blah"
Personally I think it would be nice if Java had a multi-line equivalent to Perl's HERE documents.
Why not just store your XML in an separate file and read it in to a String using Apache Commons?
String fileName = "path/to/your/file";
String fileText = FileUtils.readFileToString(new File(fileName));
See this for FileUtils - https://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With