In my class I have a method doRequest(HttpRequest, someListener)
that sends a HttpRequest
and use the listener to handle the returned HttpResponse
. Based on different responseCode
500, 203, etc, the handle method will call different method A()
, B()
.
Now I want to test whether the listener did what I want it to and verify the A()
, B()
methods execute with the correct response code. I am using Jmockit to keep consistency with existing test framework. Is there any way I can mock a fake Http response to this input listener? How can I test this listener's behavior?
Thanks a lot!
Asked
Active
Viewed 587 times
0

PiotrWolkowski
- 8,408
- 6
- 48
- 68

user3795058
- 1
- 1
-
1It would be easier to help with your issue if you showed some of your code. – PiotrWolkowski Jul 01 '14 at 19:11
1 Answers
0
To fake responses locally you can use Jaqen, a very light server built for testing scripts that depend of an API.
You set whatever you want it to respond directly on the request so it might be just what you are looking for.
For example:
# Console request to a Jaqen instance listening at localhost:9000
$ curl 'http://localhost:9000/echo.json?foo=bar&data\[name\]=Jaqen&data\[v\]=1.0.0' -i
=> HTTP/1.1 200 OK
=> Content-Type: application/json
...
=> {"foo":"bar","data":{"name":"Jaqen","v":"1.0.0"}}
There are several ways to tell it what to do and also serves static files to allow loading test pages and assets, check out the documentation for more.
It's built on node.js so it's really easy to install and use it locally.
# To install it (once you have node.js):
$ npm install -g jaqen
# To run it just type 'jaqen' on the console:
$ jaqen
=> Jaqen v1.0.0 is listening on port 9000.
=> Press Ctl+C to stop the server.
That's it!

404
- 1,372
- 1
- 10
- 6