Package com.jcabi.http.mock
Interface MkContainer
- All Superinterfaces:
AutoCloseable,Closeable
- All Known Implementing Classes:
MkGrizzlyContainer
Mock version of Java Servlet container.
A convenient tool to test your application classes against a web service. For example:
MkContainer container = new MkGrizzlyContainer()
.next(new MkAnswer.Simple(200, "works fine!"))
.start();
new JdkRequest(container.home())
.header("Accept", "text/xml")
.fetch().as(RestResponse.class)
.assertStatus(200)
.assertBody(Matchers.equalTo("works fine!"));
MatcherAssert.assertThat(
container.take().method(),
Matchers.equalTo("GET")
);
container.stop();
Keep in mind that container automatically reserves a new free TCP port
and works until JVM is shut down. The only way to stop it is to call
stop().
Since version 0.11 container implements Closeable and can be
used in try-with-resource block.
- Since:
- 0.10
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionhome()Get its home.Give this answer on the next request.Give this answer on the next request if the matcher condition is satisfied.Give this answer on the next request(s) if the matcher condition is satisfied up to a certain number of requests.intqueries()How many queries we have left.start()Start it on the first available TCP port.start(int prt) Start it on a provided port.voidstop()Stop container.take()Get the oldest request received (NoSuchElementExceptionif no more elements in the list).Get the oldest request received subject to the matching conditionGet the all requests received satisfying the given matcher
-
Method Details
-
next
Give this answer on the next request.- Parameters:
answer- Next answer to give- Returns:
- This object
-
next
Give this answer on the next request if the matcher condition is satisfied.- Parameters:
answer- Next answer to givecondition- The condition to match- Returns:
- This object
-
next
Give this answer on the next request(s) if the matcher condition is satisfied up to a certain number of requests.- Parameters:
answer- Next answer to givecondition- The condition to matchcount- Number of requests to match- Returns:
- This object
-
take
MkQuery take()Get the oldest request received (NoSuchElementExceptionif no more elements in the list).- Returns:
- Request received
-
take
Get the oldest request received subject to the matching condition. (NoSuchElementExceptionif no elements satisfy the condition).- Parameters:
matcher- The matcher specifying the condition- Returns:
- Request received satisfying the matcher
-
takeAll
Get the all requests received satisfying the given matcher. (NoSuchElementExceptionif no elements satisfy the condition).- Parameters:
matcher- The matcher specifying the condition- Returns:
- Collection of all requests satisfying the matcher, ordered from oldest to newest.
-
queries
int queries()How many queries we have left.- Returns:
- Total number of queries you can retrieve with
take() - Since:
- 1.0
-
start
Start it on the first available TCP port.- Returns:
- This object
- Throws:
IOException- If fails
-
start
Start it on a provided port.- Parameters:
prt- The port where it should start listening- Returns:
- This object
- Throws:
IOException- If fails
-
stop
void stop()Stop container. -
home
URI home()Get its home.- Returns:
- URI of the started container
-