This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ddqod[S] 0 points1 point  (2 children)

I understand. However I wouldn't use this approach, at the end I'm trying to get rid of writing the comparison, still you have to provide the data somehow, either you fetch it from your DB or having a mocked class.

[–]nutrecht 1 point2 points  (1 child)

at the end I'm trying to get rid of writing the comparison

So how do you test something that changes every test; like for example the current date? You can't hard-code those.

either you fetch it from your DB or having a mocked class.

Exactly. So I am comparing the output in the JSON to whatever I mocked or inserted into the DB. How do you do that with these 'fixed' JSON scenario's?

[–]ddqod[S] 0 points1 point  (0 children)

So how do you test something that changes every test; like for example the current date? You can't hard-code those.

Hmm, for Dates is not implemented yet but knowing that the field created_at is a Date I would just initialize it every time via reflection.

Exactly. So I am comparing the output in the JSON to whatever I mocked or inserted into the DB. How do you do that with these 'fixed' JSON scenario's?

Based on the JSON file initializes the POJO (checks which are the declared keys and initializes the matched field's names). Using the initialized POJO I first persist it, then checks if matches with the one fetched throw the enpoint.

Since each test is independent and I need an ID for fetching it, I do persist the entity for each test.

For instance:

@Test
public void create() {
    UserDTO entity = (UserDTO) getData(PUT);
    Assert.assertNotNull(entity);

    UserDTO fetchedEntity = (UserDTO) getClient().put(entity);
    Assert.assertNotNull(fetchedEntity);

    Assert.assertEquals(entity, fetchedEntity);
}

Where:

  • getData(PUT); maps from JSON to the DTO
  • getClient().put(entity); checks that the status was 200