incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Sling Website > Testing Sling-based applications
Date Tue, 08 May 2012 00:19:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=SLINGxSITE&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/SLINGxSITE/Testing+Sling-based+applications">Testing
Sling-based applications</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bdelacretaz">Bertrand
Delacretaz</a>
    </h4>
        <br/>
                         <h4>Changes (11)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-unchanged" >Automated testing of OSGi components
and services can be challenging, as many of them depend on other services that must be present
or simulated for testing. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >This page <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">explains</span>
<span class="diff-added-words"style="background-color: #dfd;">describes</span>
the various approaches that we use to test Sling itself, and introduces a number of tools
that can help testing OSGi and HTTP-based applications. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{toc}
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >h1. Unit tests <br></td></tr>
            <tr><td class="diff-changed-lines" >When possible, unit tests are
obviously the fastest executing ones, and it&#39;s easy to keep <span class="diff-changed-words">the<span
class="diff-added-chars"style="background-color: #dfd;">m</span></span> close
to the code that they&#39;re testing. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" >We have quite a lot of those in
Sling, the older use the JUnit3 TestCase base class, and later ones use JUnit4 annotations.
<span class="diff-added-words"style="background-color: #dfd;">Mixing both approaches
is possible, there&#39;s no need to rewrite existing tests.</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h1.
Mock services <br>The next step is to use mock services to test components without requiring
an OSGi runtime. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h1.
Tests that use a JCR repository <br>Utility classes from our [commons/testing|https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/testing]
module make it easy to get a real JCR repository for testing. That&#39;s a bit slower
than pure unit tests, of course, but this only adds 1-2 seconds to the execution of a test
suite. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
{{RepositoryProviderTest}} in that module uses this technique to get a JCR repository. <br>
<br>Note that our utilities do not cleanup the repository between tests, so you must
be careful about test isolation, for example by using unique paths for each test. <br>
<br>h1. Mock classes and services <br>The next step is to use mock classes services
to simulate components that are needed for testing. This makes it possible to test OSGi service
classes without an OSGi framework. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >We have a number of custom-written
mock services in Sling, like [MockNodeType|https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/MockNodeType.java]
for example. These handwritten mocks implement just what&#39;s needed for their tests,
so they might not be reusable as is. <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >[Pax Exam|http://team.ops4j.org/wiki/display/paxexam/Pax+Exam]
allows you to easily start an OSGi framework during execution of a JUnit test suite. <br>
<br></td></tr>
            <tr><td class="diff-changed-lines" >We currently use it for our [Sling
installer integration tests|https://svn.apache.org/repos/asf/sling/trunk/installer/it] for
example. As parts <span class="diff-added-words"style="background-color: #dfd;">of</span>
the installer <span class="diff-changed-words">interact<span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">s</span></span>
directly with the OSGi framework, it felt safer to test it in a realistic situation rather
than mock everything. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Such tests are obviously
slower than plain unit tests and tests that use mocks. Our installer integration tests, using
Pax Exam, take about a minute to execute on a 2010 macbook pro. <br> <br>h1. Sling
testing tools: server-side JUnit tests <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
[Sling testing tools|SLINGxSITE:Sling Testing Tools] include a module that executes JUnit
tests server-side, in a Sling instance. This allows integration tests to run in a realistic
environment, and could also be used for self-testing production systems. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">As
I write this, we are not using those tools to test Sling itself, but we have a [complete example|https://svn.apache.org/repos/asf/sling/trunk/testing/samples/integration-tests]
of integration tests that use them to run server-side JUnit tests, including automatic setup
of the test Sling instance. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h1. HTTP-based integration tests <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
highest level of integration is testing a complete Sling instance via its HTTP interface.
<br> <br>We use this technique to test Sling itself: the [launchpad/integration-tests|https://svn.apache.org/repos/asf/sling/trunk/launchpad/integration-tests]
module defines the tests (462 of them as I write this), and the [launchpad/testing|https://svn.apache.org/repos/asf/sling/trunk/launchpad/testing]
module executes them, after setting up a Sling instance from scratch (which is quite easy
as Sling is just a runnable jar).  <br> <br>A simple mechanism (described in README
files in these modules) allows individual tests to be executed quickly against a previously
started Sling instance, to be able to write and debug tests efficiently. <br> <br>The
test code could be made simpler using the fluent HTTP interfaces defined in the Sling testing
tools described above, but the launchpad tests were written before that module was created,
and as they&#39;re stable there&#39;s no reason to rewrite them. If you&#39;re
planning on using this technique for your own applications, we recommend looking at the Sling
testing tools instead of these &quot;legacy&quot; tests - but the basic technique
is the same. <br> <br>One problem with these launchpad tests is that the tests
of all Sling modules are defined in a single testing modules, the tests are not co-located
with the code that they test. This could be improved using the Sling testing tools, by providing
the tests in bundles that can be created from the same Maven modules that the code that they
test. <br> <br>h1. Summary <br>Combining the above testing techniques has
worked well for us in creating and testing Sling. Being able to test things at different levels
of integrations has proved an efficient way to get good test coverage without having to write
too much (boring) test code. <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Automated testing of OSGi components and services can be challenging, as
many of them depend on other services that must be present or simulated for testing.</p>

<p>This page describes the various approaches that we use to test Sling itself, and
introduces a number of tools that can help testing OSGi and HTTP-based applications.</p>

<div>
<ul>
    <li><a href='#TestingSling-basedapplications-Unittests'>Unit tests</a></li>
    <li><a href='#TestingSling-basedapplications-TeststhatuseaJCRrepository'>Tests
that use a JCR repository</a></li>
    <li><a href='#TestingSling-basedapplications-Mockclassesandservices'>Mock
classes and services</a></li>
<ul>
    <li><a href='#TestingSling-basedapplications-Injectingservicesinprivatefields'>Injecting
services in private fields</a></li>
</ul>
    <li><a href='#TestingSling-basedapplications-PaxExam'>Pax Exam</a></li>
    <li><a href='#TestingSling-basedapplications-Slingtestingtools%3AserversideJUnittests'>Sling
testing tools: server-side JUnit tests</a></li>
    <li><a href='#TestingSling-basedapplications-HTTPbasedintegrationtests'>HTTP-based
integration tests</a></li>
    <li><a href='#TestingSling-basedapplications-Summary'>Summary</a></li>
</ul></div>

<h1><a name="TestingSling-basedapplications-Unittests"></a>Unit tests</h1>
<p>When possible, unit tests are obviously the fastest executing ones, and it's easy
to keep them close to the code that they're testing. </p>

<p>We have quite a lot of those in Sling, the older use the JUnit3 TestCase base class,
and later ones use JUnit4 annotations. Mixing both approaches is possible, there's no need
to rewrite existing tests.</p>

<h1><a name="TestingSling-basedapplications-TeststhatuseaJCRrepository"></a>Tests
that use a JCR repository</h1>
<p>Utility classes from our <a href="https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/testing"
class="external-link" rel="nofollow">commons/testing</a> module make it easy to get
a real JCR repository for testing. That's a bit slower than pure unit tests, of course, but
this only adds 1-2 seconds to the execution of a test suite.</p>

<p>The <tt>RepositoryProviderTest</tt> in that module uses this technique
to get a JCR repository.</p>

<p>Note that our utilities do not cleanup the repository between tests, so you must
be careful about test isolation, for example by using unique paths for each test.</p>

<h1><a name="TestingSling-basedapplications-Mockclassesandservices"></a>Mock
classes and services</h1>
<p>The next step is to use mock classes 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"
class="external-link" rel="nofollow">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>In other cases we use <a href="http://www.jmock.org/" class="external-link" rel="nofollow">jmock</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/"
class="external-link" rel="nofollow">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>

<h2><a name="TestingSling-basedapplications-Injectingservicesinprivatefields"></a>Injecting
services in private fields</h2>
<p>To inject (real or fake) services in others for testing, without having to create
getters and setters just for this, we use a reflection-based trick, as in this example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>setting a private field via reflection</b></div><div
class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// set resource resolver factory
</span><span class="code-comment">// in a ServletResolver object which has a <span
class="code-keyword">private</span> resourceResolverFactory field
</span>
ServletResolver servletResolver = ....
<span class="code-object">Class</span>&lt;?&gt; resolverClass = servletResolver.getClass().getSuperclass();
<span class="code-keyword">final</span> java.lang.reflect.Field resolverField
= resolverClass.getDeclaredField(<span class="code-quote">"resourceResolverFactory"</span>);
resolverField.setAccessible(<span class="code-keyword">true</span>);
resolverField.set(servletResolver, factory);
</pre>
</div></div>

<h1><a name="TestingSling-basedapplications-PaxExam"></a>Pax Exam</h1>
<p><a href="http://team.ops4j.org/wiki/display/paxexam/Pax+Exam" class="external-link"
rel="nofollow">Pax Exam</a> allows you to easily start an OSGi framework during execution
of a JUnit test suite.</p>

<p>We currently use it for our <a href="https://svn.apache.org/repos/asf/sling/trunk/installer/it"
class="external-link" rel="nofollow">Sling installer integration tests</a> for example.
As parts of the installer interact directly with the OSGi framework, it felt safer to test
it in a realistic situation rather than mock everything.</p>

<p>Such tests are obviously slower than plain unit tests and tests that use mocks. Our
installer integration tests, using Pax Exam, take about a minute to execute on a 2010 macbook
pro.</p>

<h1><a name="TestingSling-basedapplications-Slingtestingtools%3AserversideJUnittests"></a>Sling
testing tools: server-side JUnit tests</h1>
<p>The <a href="/confluence/display/SLINGxSITE/Sling+Testing+Tools" title="Sling
Testing Tools">Sling testing tools</a> include a module that executes JUnit tests
server-side, in a Sling instance. This allows integration tests to run in a realistic environment,
and could also be used for self-testing production systems.</p>

<p>As I write this, we are not using those tools to test Sling itself, but we have a
<a href="https://svn.apache.org/repos/asf/sling/trunk/testing/samples/integration-tests"
class="external-link" rel="nofollow">complete example</a> of integration tests that
use them to run server-side JUnit tests, including automatic setup of the test Sling instance.</p>

<h1><a name="TestingSling-basedapplications-HTTPbasedintegrationtests"></a>HTTP-based
integration tests</h1>
<p>The highest level of integration is testing a complete Sling instance via its HTTP
interface.</p>

<p>We use this technique to test Sling itself: the <a href="https://svn.apache.org/repos/asf/sling/trunk/launchpad/integration-tests"
class="external-link" rel="nofollow">launchpad/integration-tests</a> module defines
the tests (462 of them as I write this), and the <a href="https://svn.apache.org/repos/asf/sling/trunk/launchpad/testing"
class="external-link" rel="nofollow">launchpad/testing</a> module executes them,
after setting up a Sling instance from scratch (which is quite easy as Sling is just a runnable
jar). </p>

<p>A simple mechanism (described in README files in these modules) allows individual
tests to be executed quickly against a previously started Sling instance, to be able to write
and debug tests efficiently.</p>

<p>The test code could be made simpler using the fluent HTTP interfaces defined in the
Sling testing tools described above, but the launchpad tests were written before that module
was created, and as they're stable there's no reason to rewrite them. If you're planning on
using this technique for your own applications, we recommend looking at the Sling testing
tools instead of these "legacy" tests - but the basic technique is the same.</p>

<p>One problem with these launchpad tests is that the tests of all Sling modules are
defined in a single testing modules, the tests are not co-located with the code that they
test. This could be improved using the Sling testing tools, by providing the tests in bundles
that can be created from the same Maven modules that the code that they test.</p>

<h1><a name="TestingSling-basedapplications-Summary"></a>Summary</h1>
<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 integrations has proved an efficient
way to get good test coverage without having to write too much (boring) test code.</p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/SLINGxSITE/Testing+Sling-based+applications">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=27846122&revisedVersion=5&originalVersion=4">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/SLINGxSITE/Testing+Sling-based+applications?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message