15

I am trying to learn Kotlin, and test how it works with spring boot. My application is using a mongo database to store data and I have a Jersey resource for retrieving data. I am testing it using spring-boot-test and RestTestTemplate.

The RestTestTemplate has an exchange method which takes a ParameterizedTypeReference. This class has a protected constructor. So the only way I managed to use it from Kotlin was like this:

class ListOfPeople : ParameterizedTypeReference<List<Person>>()

Here is my test-method:

@Test
fun `get list of people`() {
    // create testdata
    datastore.save(Person(firstname = "test1", lastname = "lastname1"))
    datastore.save(Person(firstname = "test2", lastname = "lastname2"))
    datastore.save(Person(firstname = "test3", lastname = "lastname2"))
    datastore.save(Person(firstname = "test4", lastname = "lastname2"))

    val requestEntity = RequestEntity<Any>(HttpMethod.GET, URI.create("/person"))

    // create typereference for response de-serialization
    class ListOfPeople : ParameterizedTypeReference<List<Person>>() // can this be done inline in the exchange method?
    val responseEntity : ResponseEntity<List<Person>> = restTemplate.exchange(requestEntity, ListOfPeople())

    assertNotNull(responseEntity)
    assertEquals(200, responseEntity.statusCodeValue)
    assertTrue( responseEntity.body.size >= 4 )

    responseEntity.body.forEach { person ->
        println("Found person: [${person.firstname} ${person.lastname}] " +
                ", born [${person.birthdate}]")
    }
}

Is this the correct (or only) way to do this, or is there a better way?

If it helps, here is a link for the whole test: testclass on github

thomas77
  • 1,100
  • 13
  • 27
  • 1
    https://kotlinlang.org/docs/reference/nested-classes.html#anonymous-inner-classes – JB Nizet Sep 30 '18 at 20:15
  • Thanks! I had read that earlier today, but couldn´t get it to work. Tried it now and this: restTemplate.exchange(requestEntity, object: ParameterizedTypeReference> () {}) worked. Thank you! – thomas77 Sep 30 '18 at 20:19

2 Answers2

30

While the answer using object expression is correct and the direct equivalent of the way you do it in Java, reified type parameters allow you to simplify it if you need many ParameterizedTypeReferences:

inline fun <reified T> typeReference() = object : ParameterizedTypeReference<T>() {}

// called as
restTemplate.exchange(requestEntity, typeReference<List<Person>>())

When the compiler sees a typeReference<SomeType> call, it's replaced by the definition, so the result is the same as if you wrote object : ParameterizedTypeReference<SomeType>() {}.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 1
    Thanks. I accept this answer as I consider it better. I can absolutely see use-cases when I would need more than one :) – thomas77 Oct 01 '18 at 07:13
8

Thanks to JB Nizet who pointed me to the correct documentation.

val responseEntity : ResponseEntity<List<Person>> =
            restTemplate.exchange(requestEntity, 
            object: ParameterizedTypeReference<List<Person>> () {})

If I read correctly this is called an Object expression.

thomas77
  • 1,100
  • 13
  • 27
  • 2
    Thank you, but still doesn't work for me. The inner model (here: Person) is just parsed as a LinkedHashMap for some reason. – spyro Jan 14 '21 at 10:26