Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 948
slayton Avatar asked Feb 01 '26 01:02

slayton


1 Answers

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>>(){});
like image 116
Andrew Avatar answered Feb 02 '26 14:02

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!