-
Notifications
You must be signed in to change notification settings - Fork 20
18. Testing
JakeHoward edited this page Jul 16, 2015
·
1 revision
Unit testing resources is really easy and doesn't require any special test setup. Resources are plain Java objects.
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.ResponseBuilder;
import com.googlecode.utterlyidle.Status;
import com.googlecode.utterlyidle.annotations.GET;
import com.googlecode.utterlyidle.annotations.Path;
import com.googlecode.utterlyidle.annotations.QueryParam;
public class HelloResource {
@GET
@Path("hello")
public Response hello(@QueryParam("param") String param) {
return ResponseBuilder.response(Status.OK).entity(param).build();
}
}import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.ResponseBuilder;
import com.googlecode.utterlyidle.Status;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
public class HelloResourceTest {
@Test
public void getHello() {
HelloResource resource = new HelloResource();
Response response = resource.hello("test");
assertEquals(ResponseBuilder.response(Status.OK).entity("test").build(), response);
}
}Please note that we can use equals method provided by the Utterlyidle Response implementation in order to build the expected response in a test.
import com.googlecode.utterlyidle.HttpHandler;
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.ServerConfiguration;
import com.googlecode.utterlyidle.Status;
import com.googlecode.utterlyidle.handlers.ClientHttpHandler;
import com.googlecode.utterlyidle.httpserver.RestServer;
import org.junit.After;
import org.junit.Test;
import java.io.IOException;
import static com.googlecode.utterlyidle.BasePath.basePath;
import static com.googlecode.utterlyidle.RequestBuilder.get;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class HelloApplicationTest {
private RestServer server;
private HelloApplication application = new HelloApplication(basePath("/"));
@Test
public void shouldSayHello_withServer() throws Exception {
server = new RestServer(application, ServerConfiguration.defaultConfiguration());
HttpHandler httpClient = new ClientHttpHandler(2000);
Response response = httpClient.handle(get(server.uri().toString() + "hello").build());
assertThat(response.status(), is(Status.OK));
}
@After
public void tearDown() throws IOException {
if(server != null) server.close();
}
@Test
public void shouldSayHello_withoutServer() throws Exception {
Response response = application.handle(get("/hello").build());
assertThat(response.status(), is(Status.OK));
}
}Many functional tests may be performed with a simple HTTP client and an embedded server. If you want to test some API or a website where JS is optional it's a nice alternative to the slower browser automation tools. Because Utterlyidle has a uniform client/server API you can also run the same tests directly against the application instance without starting a server.
TODO:
- replacing dependencies for testing