lundi 28 décembre 2020

Convert data class to map to test http GET response body

I'm trying to test a GET to get all the StatusMapping objects created, however, I'm not sure what's the best approach to test this.

The response is returning a map whereas I was expecting a list of StatusMapping objects instead. Should I convert the requests to a map?


Here's the Service code:

fun getAll(): ResponseEntity<List<StatusMapping>> {
    return ResponseEntity<List<StatusMapping>>(statusMappingRepository.findAll(), HttpStatus.OK)
}

Here's the test

@Test
fun `Get all mappings created`() {
    val requests = listOf(
        StatusMapping("available", "available"),
        StatusMapping("unavailable", "unavailable")
    )
    requests.forEach { statusMappingService.createMapping(it.toStatusMappingRequest()) }

    val response = restTemplate.getForEntity(getRootUrl(), List::class.java)

    assertEquals(response.body, requests)
}

Here's the error that I'm getting:

Expected :[{source=available, target=available}, {source=unavailable, target=unavailable}]
Actual   :[StatusMapping(source=available, target=available), StatusMapping(source=unavailable, target=unavailable)]

Aucun commentaire:

Enregistrer un commentaire