If you have completed the steps from these posts, you should have a running application in the beginning of your tests. And now you are ready to start the actual testing of the application.
I believe that the best way to test RESTful API is to issue actual HTTP requests and since we have a Jetty server running, it becomes possible.
There are a lot of HTTP Clients available in Java. The examples below use Apache HTTP Client.
But as always first let's add a maven dependency:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
Now some convenient static methods that can be used:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicHeader;
...
static HttpResponse executeGet(String url) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
return httpclient.execute(get);
}
static HttpResponse executeDelete(String url) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpDelete delete = new HttpDelete(url);
return httpclient.execute(delete);
}
static HttpResponse executePost(String url, byte[] body) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setEntity(new ByteArrayEntity(body));
return httpclient.execute(post);
}
static HttpResponse executePut(String url, byte[] body) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPut put = new HttpPut(url);
put.setEntity(new ByteArrayEntity(body));
return httpclient.execute(put);
}
So your test can be something like:
@Test
public void testGet() throws Exception {
HttpResponse response = executeGet(url, null, null, null);
assertEquals(response.getStatusLine().getStatusCode(), 200);
}
Recommended Reading
1. Next Generation Java Testing: TestNG and Advanced Concepts2. Apache Maven 3 Cookbook
3. Spring Recipes: A Problem-Solution Approach
No comments:
Post a Comment