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
);
}
}
|
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 @Poll
annotation. 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
);
}
}
|
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
);
}
}
|
Convinced?
Download restfuse or take a look at the API.
- About EclipseSource
- Imprint
- Privacy
- Terms of Use
- Contact Us