Allows providing stubs (or mocks) by implementing Groovy script basing on Moco framework.
Sample stub mapping (hello-world.stub.json):
[
{
"request" :
{
"uri" : "/hello-world"
},
"response" :
{
"text" : "Hello! I am sample stub."
}
}
]
Sample stub script (hello-world.stub.groovy):
stubs.server
.get(by(uri("/hello-world")))
.response("Hello! I am sample stub.")
stubs.server
.get(by(uri("/read_query_file")))
.response(jcr(template('/conf/stubs/moco/samples/${req.queries["id"]}.json')))
stubs.server
.get(
and(
by(uri("/secured")),
eq(query("password"), "secret")
)
)
.response("Secured endpoint revealed!")
stubs.server
.get(by(uri("/current-date")))
.response(template('Today date is "${date}"', "date", suppliedVar({ new Date() })))
Download appropriate distribution from releases section:
- moco-all - Moco framework only with Groovy OSGi bundle included,
- moco-app - Moco framework only without Groovy OSGi bundle included.
- all - Moco and WireMock frameworks with Groovy OSGi bundle included,
- app - Moco and WireMock frameworks without Groovy OSGi bundle included.
To create stubs for web services or more basically mock any data, it is needed to only create a stub script under pre-configured path, e.g /conf/stubs/moco/hello.groovy.
stubs.server
.get(by(uri("/hello-world")))
.response("Hello! I am sample stub.")
Above stub will be accessible at URL http://localhost:5555/hello-world. Note that port 5555 is configurable via OSGi configuration.
Be aware of common / Moco agnostic OSGi configuration at first.
Then check see Moco specific OSGi configuration below:
At first, see common script API.
Pre-defined variables:
- stubs.server - use it to define Moco stubs.
Default imports (not needed to be specified explicitly in scripts):
- com.github.dreamhead.moco.Moco.* - base Moco API,
- com.cognifide.aem.stubs.moco.MocoUtils.* - AEM Stubs related extensions for Moco API.
None.