0

I am trying to write a test case for the REST API part of my website. I use dropwizard.

@Test
public void getAll(){
    assertThat(resources.client().target("/blogcomments").request().get(List.class))
            .isEqualTo(GsonInstance.INSTANCE.getGson().toJson(list));
    verify(dao).getAll(list);
}

And what I get is like this :

[{"comment"="comment", "date"=1464906153750L, "id"=1, "name"="name", "url"="url"}]

In the assert above, if I use an object to compare, actually I am using [api.BlogComment@e3e181ab].

If I use Gson to convert object to json like this:

@Test
public void getAll(){
    assertThat(resources.client().target("/blogcomments").request().get(List.class))
            .isEqualTo(GsonInstance.INSTANCE.getGson().toJson(list));
    verify(dao).getAll(POJOs.BlogCommentPOJO.toString());
}

What I got is:

org.junit.ComparisonFailure: Expected :"[{"id":1,"url":"url","comment":"comment","date":"Jun 2, 2016 6:27:50 PM","name":"name"}]" Actual :[{"comment"="comment", "date"=1464906470531L, "id"=1, "name"="name", "url"="url"}]

How to deal with the Date format? How to compare whether two JSON are equal?

SmartFingers
  • 105
  • 1
  • 8

1 Answers1

1

Firstly, you seem to be trying to compare String representations of actuals and expected values. I would suggest you override hashCode() and equals() methods of your POJO and it's inner classes. Then compare the objects in your test cases by simply using assertEquals. (Also override toString for debuggability)

However, for some reason if you need compare String representations, you may do as below: (I wouldn't recommend it, though)

Community
  • 1
  • 1
Manu Manjunath
  • 6,201
  • 3
  • 32
  • 31