0

How can I make a test case like this more readable?

@Test
public void test1() throws Exception {
    assertEquals("{\"Root\":{\"Name\":\"Paul\",\"Age\":\"10\",\"Enabled\":\"true\"}}",
            transformer.transformXmlToJson("<Root><Name>Paul</Name><Age>10</Age><Enabled>true</Enabled></Root>"));
}

Do I need to manually put line breaks in the String like,

@Test
public void test1() throws Exception {
    assertEquals(
            "{\"Root\":"
            + "{"
            + "\"Name\":\"Paul\","
            + "\"Age\":\"10\","
            + "\"Enabled\":\"true\""
            + "}"
            + "}",
            transformer.transformXmlToJson(
                    "<Root>"
                    + "<Name>Paul</Name>"
                    + "<Age>10</Age>"
                    + "<Enabled>true</Enabled>"
                    + "</Root>"));
}

But when I format the class in eclipse, the whole formats goes for a toss.

Is there a good practice, we should follow, when writing these test cases to make it more readable?

Similarly, is there a way to write the JSON in a better format?

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
Paul Nibin
  • 726
  • 3
  • 10
  • 18

3 Answers3

1

What I would personally do, is load the XML or JSON data from resource files. Something like :

src
+-- test
    +-- java
    |   +-- mypackage
    |       +-- MyTestClass.java
    \-- resources
        +-- mypackage
            +-- test1_input.xml
            +-- test1_expected_output.json

Test Class (using commons-io, but any IO library or your own code would do) :

package mypackage;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.junit.Test;

public class MyTestClass {

    @Test
    public void test1() throws IOException {
        final String xmlInput = IOUtils
                .toString(getClass().getResourceAsStream("test1_input.xml"));
        final String expectedJsonOutput = IOUtils
                .toString(getClass().getResourceAsStream("test1_expected_output.json"));
        // Do the test
    }
}

Of course, the test code is then easily applied to a bunch of test cases with different inputs.

Whether or not you find this a good idea in terms of readability is probably largely a matter of taste though.


Another aspect is the way you compare JSON (or XML) strings: do you really want the assertion to fail if the JSON is just formatted differently (extra spaces or line breaks between fields...)? If not, you should consider using libraries which will compare the actual strcuture of the JSON (or XML) data, such as:

  • JSONassert (for JSON)
  • hamcrest-json (for JSON, if you use Hamcrest matchers; it's based on JSONassert)
  • XMLUnit (for XML ; see the XMLAssert class)
  • And probably others...
Cyäegha
  • 4,191
  • 2
  • 20
  • 36
0

If it is just about formatting, you can see here how to disable the formatter for certain sections within a class/method ( I do that occasionally in my code, too).

But beyond that, you might consider using assertThat and to write a custom matcher for this kind of comparisons.

Correction: I strongly recommend to use assertThat in any case; independent from the question if it would make sense to create a custom matcher.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I cannot disable the formatter. Formatter is very helpful for me. Will try using assertThat(). – Paul Nibin Jun 22 '16 at 08:25
  • 1
    If you would have bothered to look up my link, you would have found that you can disable the formatter for **certain sections** in your code. Of course I do not advise to disable the formatter completely! – GhostCat Jun 22 '16 at 08:26
  • Sorry about that. Was carried over by the assertThat you were mentioning and was looking into how to use that in my test code. This could be done. But I have to ask everyone working on these tests to enable that setting in eclipse. Would look for some other solution, before doing this. – Paul Nibin Jun 22 '16 at 08:32
  • Not sure if you really got it: **you** place the formatter instructions **within** your code. Other users don't have to do anything for that. – GhostCat Jun 22 '16 at 08:39
  • They have to edit their formatter to enable the on/off tags. right? – Paul Nibin Jun 22 '16 at 08:45
  • That part is true. But to a certain degree you want that people in your team are using identical settings anyway. – GhostCat Jun 22 '16 at 08:59
0

I think the best approach would be:

  1. Create a Java class (say A.java) if there is not one already which represents the JSON and the XML. Override the equals() and hashCode() of this class.
  2. Convert the actual JSON String to a Java object (of class A) say obj1.
  3. Convert the actual XML String to a Java object (of class A) say obj2.
  4. Assert obj1 and obj2 for equality.
Sanjeev Saha
  • 2,632
  • 1
  • 12
  • 19