This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch asf-site
in repository https://gitbox.apache.org/repos/asf/sling-site.git
The following commit(s) were added to refs/heads/asf-site by this push:
new d8bbd77 Improve documentation around Sling Mocks and Sling Models
d8bbd77 is described below
commit d8bbd77b6a310d4ece19e06a6ecb88cabf998ba0
Author: Konrad Windszus <[email protected]>
AuthorDate: Fri Oct 5 12:10:41 2018 +0200
Improve documentation around Sling Mocks and Sling Models
---
documentation/development/sling-mock.html | 23 ++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/documentation/development/sling-mock.html
b/documentation/development/sling-mock.html
index 336322e..2bb1516 100644
--- a/documentation/development/sling-mock.html
+++ b/documentation/development/sling-mock.html
@@ -136,8 +136,8 @@
<li>It is possible to set request data to simulate a certain Sling HTTP
request</li>
</ul>
</li>
- <li>Support for Sling Models (Sling Models API 1.1 and Impl 1.1 or higher
required)</li>
- <li>Additional services <code>MimeTypeService</code></li>
+ <li>Support for Sling Models (Sling Models API 1.1 and Impl 1.1 or higher
required), all relevant Sling Models services are registered by default</li>
+ <li>Additional services: <code>MimeTypeService</code></li>
<li>Context Plugins</li>
</ul>
<p>The following features are <em>not supported</em>:</p>
@@ -284,6 +284,23 @@ ResourceResolver resolver =
MockSling.newResourceResolver();
ResourceResolver resolver =
MockSling.newResourceResolver(ResourceResolverType.JCR_MOCK);
</code></pre>
<p>If you use the <code>SlingContext</code> JUnit rule you case just use
<code>context.resourceResolver()</code>.</p>
+<h3><a href="#sling-models" name="sling-models">Sling Models</a></h3>
+<p>You should use the following approach to test Sling Models.</p>
+<h4><a href="#model-registration" name="model-registration">Model
Registration</a></h4>
+<p>First you need to make sure that the model you want to test is registered.
Since Sling Mocks 1.9.0/2.2.0 the Sling Models from the classpath are
automatically registered (<a
href="https://issues.apache.org/jira/browse/SLING-6363">SLING-6363</a>) when
the Manifest contains the right bundle headers
(<code>Sling-Model-Packages</code> or <code>Sling-Model-Classes</code>). This
behaviour can be tweaked since version 2.2.20 with the
<code>SlingContextBuilder.registerSlingModelsFromClassPath [...]
+<p>Manual registration is supported via
<code>SlingContext.addModelsForPackage(...)</code> and
<code>SlingContext.addModelsForClasses(...)</code>.</p>
+<h4><a href="#model-instantiation" name="model-instantiation">Model
Instantiation</a></h4>
+<p>Preferably use the <code>ModelFactory.createModel(...)</code> method rather
than the adaptTo method to benefit from better error messages in case of
errors.</p>
+<pre><code><!-- TODO syntax marker (#!java) disabled -->// load some content
into the mocked repo
+context.load().json(..., "/resource1");
+
+// load resource
+Resource myResource =
content.resourceResolver().getResource("/resource1");
+
+// instantiate Sling Model (adaptable via Resource)
+// this will throw exceptions if model cannot be instantiated
+MyModel myModel =
context.getService(ModelFactory.class).createModel(myResource, MyModel.class);
+</code></pre>
<h3><a href="#adapter-factories" name="adapter-factories">Adapter
Factories</a></h3>
<p>You can register your own or existing adapter factories to support
adaptions e.g. for classes extending <code>SlingAdaptable</code>.</p>
<p>Example:</p>
@@ -451,7 +468,7 @@ assertArrayEquals(TEST_DATA, response.getOutput());
</ul></section></div></div>
<div class="footer">
<div class="revisionInfo">
- Last modified by <span class="author">sseifert</span> on
<span class="comment">Mon Aug 13 17:34:05 2018 +0200</span>
+ Last modified by <span class="author">Konrad
Windszus</span> on <span class="comment">Fri Oct 5 09:40:42 2018 +0200</span>
</div> <p>
Apache Sling, Sling, Apache, the Apache feather logo, and
the Apache Sling project logo are trademarks of The Apache Software Foundation.
All other marks mentioned may be trademarks or registered trademarks of their
respective owners.
</p><p>