原文见:http://thoughts.inphina.com/2011/03/07/testing-rest-with-grizzly/
In my last post, we had seen how easy it was to create REST based services with Spring and Jersey. In this post we would quickly see how we can unit test them. Ok, this is not entirely a unit test in the strict sense because we are not testing the resource in isolation but it is a good strategy nevertheless because it helps us to test the resource right from the IDE.
For this, we would include the Grizzly servlet webserver dependency in our pom.xml.
2 |
< groupId >com.sun.grizzly</ groupId >
|
3 |
< artifactId >grizzly-servlet-webserver</ artifactId >
|
4 |
< version >1.9.8</ version >
|
Once we have the Grizzly server, we would have to power it up at the start of our tests so that the resource can execute inside the container. The test start-up would look like this
2 |
public void setUp() throws Exception {
|
3 |
threadSelector = GrizzlyMain.startServer();
|
4 |
ClientConfig clientConfiguration = new DefaultClientConfig();
|
5 |
Client client = Client.create(clientConfiguration);
|
6 |
webresource = client.resource(GrizzlyMain.baseUri);
|
Also, at the end of every test, we have to terminate the endpoint so that the port is now available for the next test. Hence, we do
2 |
public void tearDown() throws Exception {
|
3 |
threadSelector.stopEndpoint();
|
If you would notice, we have started the GrizzlyServer with GrizzlyMain.startServer(); Let us see what this class looks like
01 |
public class GrizzlyMain {
|
03 |
private static int getPort( int defaultPort) {
|
04 |
String port = System.getenv( "JERSEY_HTTP_PORT" );
|
07 |
return Integer.parseInt(port);
|
08 |
} catch (NumberFormatException e) {
|
14 |
final static URI baseUri = UriBuilder.fromUri( "http://localhost/" ).port( 9998 ).build();
|
16 |
public static SelectorThread startServer() throws IOException{
|
17 |
final ServletAdapter adapter = new ServletAdapter();
|
18 |
adapter.addInitParameter( "com.sun.jersey.config.property.packages" , "com.inphina.sample" );
|
19 |
adapter.addInitParameter( "com.sun.jersey.api.json.POJOMappingFeature" , "true" );
|
20 |
adapter.addContextParameter( "contextConfigLocation" , "classpath:applicationContext.xml" );
|
21 |
adapter.addServletListener( "org.springframework.web.context.ContextLoaderListener" );
|
22 |
adapter.setServletInstance( new SpringServlet() );
|
23 |
adapter.setContextPath(baseUri.getPath());
|
24 |
adapter.setProperty( "load-on-startup" , 1 );
|
26 |
System.out.println( "********" + baseUri.getPath());
|
27 |
SelectorThread threadSelector = GrizzlyServerFactory.create(baseUri, adapter);
|
28 |
return threadSelector;
|
31 |
public static void main(String[] args) throws IOException {
|
32 |
System.out.println( "Starting grizzly..." );
|
33 |
SelectorThread threadSelector = startServer();
|
34 |
System.out.println(String.format(
|
35 |
"Jersey app started with WADL available at %sapplication.wadl\n" +
|
36 |
"Hit enter to stop it..." , baseUri));
|
38 |
threadSelector.stopEndpoint();
|
As you would notice, the startServer() method, starts the server along with all the details which are present in our web.xml (refer to the last post). Here, we register the SpringServlet and also pass all the init parameters to the servlet. A snapshot of the web.xml is shown here
02 |
< servlet-name >REST</ servlet-name >
|
04 |
com.sun.jersey.spi.spring.container.servlet.SpringServlet</ servlet-class >
|
06 |
< param-name >com.sun.jersey.config.property.packages</ param-name >
|
07 |
< param-value >com.inphina.social.resources</ param-value >
|
10 |
< param-name >com.sun.jersey.api.json.POJOMappingFeature</ param-name >
|
11 |
< param-value >true</ param-value >
|
13 |
< load-on-startup >1</ load-on-startup >
|
Now, let us look at a sample test
2 |
public void getOnHelloWorldPath() {
|
4 |
String s = webresource.path( "helloworld" ).accept( "application/json" ).get(String. class );
|
5 |
Assert.assertEquals( "{\"name\":\"Vikas Hazrati\",\"age\":36,\"email\":\"vhazrati@inphina.com\"}" , s);
|
Here, we make a GET call at the path helloworld and expect a JSON string to be returned which should be equal to what we are expecting. Likewise to make a sample post call you would have a method like this
02 |
public void testPostLoginJSONFormat() {
|
03 |
User user = new User();
|
04 |
user.setEmail( "admin" );
|
05 |
user.setPassword( "admin" );
|
06 |
Address address = new Address();
|
07 |
address.setStreet( "123" );
|
08 |
user.addAddress(address);
|
10 |
webresource.path( "helloworld" ).type( "application/json" ).post(user);
|
Hence, it is easy to test the REST services with the lightweight Grizzly server in place. Thecomplete code for this and previous post can be accessed here.
http://justrest.googlecode.com/svn/trunk/ justrest-read-only
分享到:
相关推荐
春天休息服务器 具有Spring集成的简单REST服务器 以下是创建不同类型的Jersey项目的方法: 创建一个示例Jersey 2.3.1项目,该项目生成一...创建带有嵌入式Grizzly Web服务器的示例Jersey 2.3.1项目: mvn archetype:g
Jersey是Java的一个开源项目,它实现了Java API for RESTful Web Services (JAX-RS),这是一个标准接口,使得在Java平台上创建RESTful服务变得更加简单。以下是对压缩包中每个文件的基本解释和相关知识点: 1. **...
其次,Jersey是JAX-RS(Java API for RESTful Web Services)的参考实现,它提供了创建REST服务的API和工具。通过使用Jersey,开发者可以轻松地创建HTTP资源,处理GET、POST、PUT等HTTP方法,以及JSON和XML数据格式...
Jersey 是一个开源的 RESTful Web 服务客户端和服务器端框架,它基于 Java 的 JAX-RS(Java API for RESTful Web Services)规范。在Java应用程序中使用Jersey,需要包含一些核心的JAR包来支持其功能。以下是标题和...
为Servlet,并设置`jersey.config.server.provider.packages`属性,指示 Jersey 扫描包含REST资源的包。 接下来,我们编写REST服务接口。在Java源代码中,可以创建一个名为`MyResource.java`的类,该类通过使用JAX-...
- **Jersey与REST的关系**:Jersey实现了RESTful Web Services的核心思想和技术实践,帮助开发者更便捷地构建RESTful API。 #### 二、Jersey核心功能与特点 - **资源类与方法**:在Jersey中,可以通过定义资源类和...
使用 Java / Jersey 测试 REST API,运行嵌入式 Grizzly Web 服务器。 在 获取所有任务 GET /任务 获得一项任务 GET /tasks/{key} 创建新任务 POST /tasks/{key} { "body": "Another Body", "title": "Hello World...
标题中的“grizzly”指的是Grizzly,一个由Sun Microsystems(现为Oracle)开发的开源网络应用框架,主要用于构建高性能、可扩展的网络服务器。Grizzly是Java平台上的一个组件,它提供了一组灵活且强大的API,可以...
Java JAX-RS(Java API for RESTful Web Services)是Java平台上的标准API,用于构建RESTful服务。它提供了一种简单的方式来创建、暴露和消费RESTful Web服务。JAX-RS允许开发者使用注解来定义资源类和方法,从而将...
Jersey 是一个开源的 RESTful Web 服务客户端和服务器实现,基于 Java EE 的 JAX-RS(Java API for RESTful Web Services)规范。这个“jersey-archive-1.8.rar”压缩包包含了 Jersey 1.8 版本的所有 JAR 包,用于...
《深入理解Grizzly与Jersey:构建嵌入式Java Web服务》 在IT行业中,开发高效、轻量级的Web服务是至关重要的。Grizzly和Jersey是两个强大的Java框架,它们分别在服务器端和RESTful API开发领域扮演着重要角色。本文...
"Grizzly" 是一个轻量级的Java网络应用服务器,它提供了HTTP服务器功能,可以与Jersey结合使用来运行REST服务。此外,项目还包含了 "Swagger",这是一个用于设计、构建、文档化和使用RESTful web服务的工具,它通过...
Jersey 框架是基于 Java 的开源 RESTful Web Service 框架,它遵循 JAX-RS(Java API for RESTful Web Services)规范,并且是 JSR 311 和 JSR 339 的参考实现。RESTful 服务旨在提供一种简洁、无状态、基于标准的...
java运行依赖jar包
在 "pluralsight-asynchronous-rest-with-jersey-master" 压缩包中,你可以找到课程的源代码示例,这些示例将展示如何实际应用上述概念和技术。通过研究这些代码,你可以更深入地理解如何使用 Jersey 构建高效、异步...
`jersey-test-framework-provider-grizzly2.jar` 和 `jersey-test-framework-core.jar` 提供了内置的测试框架,可以帮助开发者在不部署到服务器的情况下测试 RESTful 服务。 综上所述,这个压缩包包含的 JAR 文件...
### Tricks and Tips with NIO Using the Grizzly Framework #### 目标与背景 在本次演讲中,我们将分享我们在开发Project Grizzly NIO框架过程中学到的一些技巧和窍门。Project Grizzly是一个利用Java NIO(非...
1. **Jersey**:Jersey是Java语言中实现RESTful Web服务的开源框架,它是JAX-RS(Java API for RESTful Web Services)规范的一个实现。通过Jersey,开发者可以轻松地创建和部署RESTful服务,提供HTTP方法如GET、...
Jersey 是一个强大的开源RESTful服务框架,它实现了Java API for RESTful Web Services(JAX-RS)规范,包括JSR 311和JSR 339这两个版本。这个框架使得开发者能够轻松地创建和部署RESTful Web服务,从而提供基于HTTP...