`
longgangbai
  • 浏览: 7250062 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

[转]An open-source JUnit extension to test HTTP/REST APIs

阅读更多

http://developer.eclipsesource.com/restfuse/

 

Everything you need to know to get started is displayed in the snippet below which is a fully functional HTTP Test.

1234567891011121314
@RunWith( HttpJUnitRunner.class )
public class RestfuseTest {
 
@Rule
public Destination destination = new Destination( "http://restfuse.com" );
 
@Context
private Response response; // will be injected after every request
 
@HttpTest( method = Method.GET, path = "/" )
public void checkRestfuseOnlineStatus() {
assertOk( response );
}
}
view rawRestfuseTest.java hosted with ❤ by GitHub

This test sends a request to restfuse.com before the test method will be executed. After the request returns the response will be injected by assigning the @Context annotated response field before the test method will be executed. This field can be used within the test method to test the content of the response. By the way, restfuse also supports the testing of asynchronous REST services.

Regardless for what you are using asynchronous mechanisms, you only have two options to deal with it: Polling or Callbacks. Both methods are supported by restfuse.

Polling

When you need to poll an asynchronous service more than once you can use the @Pollannotation. A simple example looks like the one below. The service in this example will be called 5 times as the same as the test method. The response for each request will be injected into the test object and can be tested.

Callbacks

To use a callback to test an asynchronous service you can use the @Callback annotation on you test method. Restfuse will start a server on the defined port and registers a resource. The test fails if the attached resource was not called. Within the resource you can test the incoming request. A simple callback example looks like this.

1234567891011121314151617181920
@RunWith( HttpJUnitRunner.class )
public class RestfusePollTest {
 
@Rule
public Destination destination = new Destination( "http://restfuse.com" );
 
@Context
private Response response;
 
@Context
private PollState pollState
 
@HttpTest( method = Method.GET, path = "/asynchron" )
@Poll( times = 5, interval = 500 )
public void testAsynchronousService() {
Response currentResponse = pollState.getRespone( pollState.getTimes() );
 
assertEquals( currentResponse, response );
}
}
view rawRestfusePollTest.java hosted with ❤ by GitHub
123456789101112131415161718192021222324
@RunWith( HttpJUnitRunner.class )
public class RestfuseCalbackTest {
 
@Rule
public Destination destination = new Destination( "http://restfuse.com" );
 
@Context
private Response response;
 
private class TestCallbackResource extends DefaultCallbackResource {
 
@Override
public Response post( Request request ) {
assertNotNull( request.getBody() );
return super.post( request );
}
}
 
@HttpTest( method = Method.GET, path = "/test" )
@Callback( port = 9090, path = "/asynchron", resource = TestCallbackResource.class, timeout = 10000 )
public void testMethod() {
assertAccepted( response );
}
}
  • Facebook
  • Google Plus
  • Twiter

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics