简体   繁体   中英

How to test DropWizard resource that returns a collection of objects?

I'm building a service using DropWizard. I have a endpoint that is expected to return a List<Person>

My unit tests looks like:

@Test
public void testGetListOfPeople() {

assertThat(
    resources.client().target("/people/?age=10").request().get(ArrayList<Person>.class))
    .containsAll(expectedList);
}

However, request().get won't allow me to specify a parameterized collection.

I've tried getting the response directly with:

r = resources.client().target("/people/?age=10").request().get()

but then its not clear how I convert r into a List<Person>

How can I update this test to work?

Yes, the Jersey client with collections can be a little frustrating. The solution is easy though, simply do the following:

import javax.ws.rs.core.GenericType;

resources.client().target("/people/?age=10").request()
    .get(new GenericType<List<Person>>(){});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM