一個採用junit+jersey框架的測試案例:
import org.junit.Test;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class testtools{
public void HttpRequest(String uri)
{
Client client =new Client();
WebResource webResource=client.resource(uri);
String s=webResource.get(String,class);
ClientResponse response=webResource.get(ClientResponse.class);
assertThat("",response.getEntity(String.class),notNullValue);
assertThat("",response.getStatus,equalTo(200));
}
@Test
public void test(){
HttpRequest(http://www.baidu.com);
}
}