JAX-RS: Asserting Successful Response With Response.Status.Family 📎
Response.Status.Family introduces a convenient way to assert a HTTP status of being successful (all 2xx status codes) in system tests. Instead of comparing all specific codes, you can rely on the family e.g: Response.Status.Family.SUCCESSFUL
:
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
@Test
public void crud() {
WebTarget tut = //...
Response response = tut.request(MediaType.APPLICATION_JSON).get();
assertThat(response.getStatusInfo().getFamily(), is(Response.Status.Family.SUCCESSFUL));
}
}
See you at Java EE Workshops at Munich Airport, Terminal 2, particularly in Java EE Testing and Code Quality or Virtual Dedicated Workshops / consulting