REST Client
Built upon the feature-rich Apache HttpClient library, the Juneau RestClient API adds support for fluent-style REST calls and the ability to perform marshalling of POJOs to and from HTTP parts.
Example
// Create a basic REST client with JSON support and download a bean.
MyBean bean = RestClient.create()
.json5()
.build()
.get(URI)
.run()
.assertStatus().asCode().is(200)
.assertHeader("Content-Type").matchesSimple("application/json*")
.getContent().as(MyBean.class);
REST Testing Framework
The MockRestClient class is used for performing serverless unit testing of @Rest-annotated and @Remote-annotated classes. It perform full serialization and parsing of the HTTP request and responses, but bypasses the network layer to significantly improve speed while still performing real testing.
Example
public class MockTest {
// A simple bean with one field.
public static class MyBean {
public int foo = 1;
}
// Our REST resource to test.
// Simply echos the response.
@Rest
public static class EchoRest extends BasicRestServlet {
@RestPut
public MyBean echo(@Content MyBean bean) {
return bean;
}
}
// Our JUnit test.
@Test
public void testEcho() throws Exception {
var myBean = new MyBean();
// Do a round-trip on the bean through the REST interface
myBean = MockRestClient
.create(EchoRest.class)
.json5()
.build()
.put("/echo", myBean)
.run()
.assertStatus().is(200)
.assertContent().is("{foo:1}")
.getContent().as(MyBean.class);
assertEquals(1, myBean.foo);
}
}
See Also
juneau-rest-client for more information.