mercredi 26 août 2015

Recursively verify for nulls with Mockito while navigating down object

I'm using Powermock Mockito 1.5.1 with Java 1.7.

On testing a web service method, I need to check a certain value with in the response object. The problem is, this value is a few layers deep and I'll need to do null check on all it's parent values before getting to the actual value in question.

Currently this is what I'm doing:

response = service.method1();
assertThat(response, notNullValue());
assertThat(response.getA(), notNullValue());
assertThat(response.getA().getB(), notNullValue());
assertThat(response.getA().getB().getC(), notNullValue());
assertThat(response.getA().getB().getC().getD(), notNullValue());
assertThat(response.getA().getB().getC().getD().size(), is(1));
assertThat(response.getA().getB().getC().getD().get(0), notNullValue());
assertThat(response.getA().getB().getC().getD().get(0).value(), is("ABC"));

Is there a way to save out on all the notNullValue() validation code, but have it automatically happen. So can be something like:

assertThat(response.getA().getB().getC().getD(), *recursivelyNotNull()*);
assertThat(response.getA().getB().getC().getD().size(), is(1));
assertThat(response.getA().getB().getC().getD().get(0).value(), is("ABC"));

Aucun commentaire:

Enregistrer un commentaire