You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bu...@apache.org on 2014/09/24 18:22:47 UTC
svn commit: r923409 - in /websites/staging/sling/trunk/content: ./
documentation/tutorials-how-tos/testing-sling-based-applications.html
Author: buildbot
Date: Wed Sep 24 16:22:47 2014
New Revision: 923409
Log:
Staging update by buildbot for sling
Modified:
websites/staging/sling/trunk/content/ (props changed)
websites/staging/sling/trunk/content/documentation/tutorials-how-tos/testing-sling-based-applications.html
Propchange: websites/staging/sling/trunk/content/
------------------------------------------------------------------------------
--- cms:source-revision (original)
+++ cms:source-revision Wed Sep 24 16:22:47 2014
@@ -1 +1 @@
-1626718
+1627360
Modified: websites/staging/sling/trunk/content/documentation/tutorials-how-tos/testing-sling-based-applications.html
==============================================================================
--- websites/staging/sling/trunk/content/documentation/tutorials-how-tos/testing-sling-based-applications.html (original)
+++ websites/staging/sling/trunk/content/documentation/tutorials-how-tos/testing-sling-based-applications.html Wed Sep 24 16:22:47 2014
@@ -115,6 +115,13 @@
<h2 id="mock-classes-and-services">Mock classes and services</h2>
<p>The next step is to use mock classes and services to simulate components that are needed for testing. This makes it possible to test OSGi service classes without an OSGi framework.</p>
<p>We have a number of custom-written mock services in Sling, like <a href="https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/MockNodeType.java">MockNodeType</a> for example. These handwritten mocks implement just what's needed for their tests, so they might not be reusable as is.</p>
+<p>A quite powerful mock implementation of a Sling Resource Resolver (without underlying JCR respository, but mimicking the behavior of the JCR Resource Provider) is provided in <a href="https://svn.eu.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock">testing/resourceresolver-mock</a>. It uses an in-memory map for storing the resource data, and supports reading, writing and a certain level of transaction and eventing support. Usage example:</p>
+<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>1
+2</pre></div></td><td class="code"><div class="codehilite"><pre><span class="n">MockResourceResolverFactory</span> <span class="n">factory</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MockResourceResolverFactory</span><span class="o">();</span>
+<span class="n">ResourceResolver</span> <span class="n">resolver</span> <span class="o">=</span> <span class="n">factory</span><span class="o">.</span><span class="na">getResourceResolver</span><span class="o">(</span><span class="kc">null</span><span class="o">);</span>
+</pre></div>
+</td></tr></table>
+
<p>In other cases we use <a href="http://www.jmock.org/">jmock</a> or <a href="https://code.google.com/p/mockito/">Mockito</a> to help create mock objects without having to write much code - such mocking libraries take care of the plumbing and allow you to write just the bits of code that matter (often with funny syntaxes). The tests of the <a href="https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/event/">org.apache.sling.event</a> bundle, for example, make extensive use of such mock services.</p>
<p>The problem with mocks is that it can become hard to make sure you're actually testing something, and not just "mocking mocks". At a certain level of complexity, it becomes quicker and clearer to actually start an OSGi framework for automated tests.</p>
<h3 id="side-note-injecting-services-in-private-fields">Side note: injecting services in private fields</h3>
@@ -154,7 +161,7 @@ such as the <a href="http://junit-addons
<h2 id="summary">Summary</h2>
<p>Combining the above testing techniques has worked well for us in creating and testing Sling. Being able to test things at different levels of integration has proved an efficient way to get good test coverage without having to write too much boring test code.</p>
<div class="timestamp" style="margin-top: 30px; font-size: 80%; text-align: right;">
- Rev. 1613935 by bdelacretaz on Mon, 28 Jul 2014 09:08:03 +0000
+ Rev. 1627360 by sseifert on Wed, 24 Sep 2014 16:22:37 +0000
</div>
<div class="trademarkFooter">
Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project