You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2018/08/13 15:19:04 UTC
[sling-site] branch asf-site updated: SLING-7803 JUnit5 Support for
sling-mock and osgi-mock
This is an automated email from the ASF dual-hosted git repository.
sseifert 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 6d9c053 SLING-7803 JUnit5 Support for sling-mock and osgi-mock
6d9c053 is described below
commit 6d9c053e927145f94006280bff1d427357ee2958
Author: sseifert <ss...@pro-vision.de>
AuthorDate: Mon Aug 13 17:19:02 2018 +0200
SLING-7803 JUnit5 Support for sling-mock and osgi-mock
---
documentation/development/osgi-mock.html | 65 ++++++++++++++++++++--------
documentation/development/sling-mock.html | 70 +++++++++++++++++++------------
2 files changed, 91 insertions(+), 44 deletions(-)
diff --git a/documentation/development/osgi-mock.html b/documentation/development/osgi-mock.html
index 7d2158a..834927a 100644
--- a/documentation/development/osgi-mock.html
+++ b/documentation/development/osgi-mock.html
@@ -101,16 +101,23 @@
<p><!-- TODO reactivate TOC once JBake moves to flexmark-java -->
</p>
<h2><a href="#maven-dependency" id="maven-dependency">Maven Dependency</a></h2>
+<p>For JUnit 4:</p>
<pre><code><!-- TODO syntax marker (#!xml) disabled --><dependency>
<groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
+ <artifactId>org.apache.sling.testing.osgi-mock.junit4</artifactId>
+</dependency>
+</code></pre>
+<p>For JUnit 5:</p>
+<pre><code><!-- TODO syntax marker (#!xml) disabled --><dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.osgi-mock.junit5</artifactId>
</dependency>
</code></pre>
<p>See latest version on the <a href="/downloads.cgi">downloads page</a>.</p>
<p>There are two major version ranges available:</p>
<ul>
- <li>osgi-mock 1.x: compatible with OSGi R4 and above</li>
- <li>osgi-mock 2.x: compatible with OSGi R6 and above</li>
+ <li>osgi-mock 1.x: compatible with OSGi R4 and above, JUnit 4</li>
+ <li>osgi-mock 2.x: compatible with OSGi R6 and above, JUnit 4 and JUnit 5</li>
</ul>
<h2><a href="#implemented-mock-features" id="implemented-mock-features">Implemented mock features</a></h2>
<p>The mock implementation supports:</p>
@@ -132,8 +139,17 @@
<li>Support OSGi R6 and Declarative Services 1.3: Field-based reference bindings and component property types</li>
</ul>
<h2><a href="#usage" id="usage">Usage</a></h2>
-<h3><a href="#osgi-context-junit-rule" id="osgi-context-junit-rule">OSGi Context JUnit Rule</a></h3>
-<p>The OSGi mock context can be injected into a JUnit test using a custom JUnit rule named <code>OsgiContext</code>. This rules takes care of all initialization and cleanup tasks required to make sure all unit tests can run independently (and in parallel, if required).</p>
+<p>The <code>OsgiContext</code> object provides access to mock implementations of:</p>
+<ul>
+ <li>OSGi Component Context</li>
+ <li>OSGi Bundle Context</li>
+</ul>
+<p>Additionally it supports:</p>
+<ul>
+ <li>Registering and activating OSGi services and inject dependencies</li>
+</ul>
+<h3><a href="#junit-4-osgi-context-junit-rule" id="junit-4-osgi-context-junit-rule">JUnit 4: OSGi Context JUnit Rule</a></h3>
+<p>The OSGi mock context can be injected into a JUnit test using a custom JUnit rule named <code>OsgiContext</code>. This rule takes care of all initialization and cleanup tasks required to make sure all unit tests can run independently (and in parallel, if required).</p>
<p>Example:</p>
<pre><code><!-- TODO syntax marker (#!java) disabled -->public class ExampleTest {
@@ -154,16 +170,30 @@
}
</code></pre>
-<p>It is possible to combine such a unit test with a <code>@RunWith</code> annotation e.g. for <a href="http://mockito.github.io/mockito/docs/current/org/mockito/runners/MockitoJUnitRunner.html">Mockito JUnit Runner</a>.</p>
-<p>The <code>OsgiContext</code> object provides access to mock implementations of:</p>
-<ul>
- <li>OSGi Component Context</li>
- <li>OSGi Bundle Context</li>
-</ul>
-<p>Additionally it supports:</p>
-<ul>
- <li>Registering and activating OSGi services and inject dependencies</li>
-</ul>
+<p>It is possible to combine such a unit test with a <code>@RunWith</code> annotation e.g. for <a href="https://www.javadoc.io/page/org.mockito/mockito-core/latest/org/mockito/junit/MockitoJUnitRunner.html">Mockito JUnit Runner</a>.</p>
+<h3><a href="#junit-5-osgi-context-junit-extension" id="junit-5-osgi-context-junit-extension">JUnit 5: OSGi Context JUnit Extension</a></h3>
+<p>The OSGi mock context can be injected into a JUnit test using a custom JUnit extension named <code>OsgiContextExtension</code>. This extension takes care of all initialization and cleanup tasks required to make sure all unit tests can run independently (and in parallel, if required).</p>
+<p>Example:</p>
+<pre><code><!-- TODO syntax marker (#!java) disabled -->@ExtendWith(OsgiContextExtension.class)
+public class ExampleTest {
+
+ private final OsgiContext context = new OsgiContext();
+
+ @Test
+ public void testSomething() {
+
+ // register and activate service with configuration
+ MyService service1 = context.registerInjectActivateService(new MyService(),
+ "prop1", "value1");
+
+ // get service instance
+ OtherService service2 = context.getService(OtherService.class);
+
+ }
+
+}
+</code></pre>
+<p>It is possible to combine such a unit test with a <code>@ExtendWith</code> annotation e.g. for <a href="https://www.javadoc.io/page/org.mockito/mockito-junit-jupiter/latest/org/mockito/junit/jupiter/MockitoExtension.html">Mockito JUnit Jupiter Extension</a>.</p>
<h3><a href="#getting-osgi-mock-objects" id="getting-osgi-mock-objects">Getting OSGi mock objects</a></h3>
<p>The factory class <code>MockOsgi</code> allows to instantiate the different mock implementations.</p>
<p>Example:</p>
@@ -222,8 +252,7 @@ myServiceConfig.update(props);
<p>To define a plugin implement the <code>org.apache.sling.testing.mock.osgi.context.ContextPlugin<OsgiContextImpl></code> interface. For convenience it is recommended to extend the abstract class <code>org.apache.sling.testing.mock.osgi.context.AbstractContextPlugin<OsgiContextImpl></code>. These plugins can be used with OSGi Mock context, but also with context instances deriving from it like Sling Mocks and AEM Mocks. In most cases you would just override the <code>afterSet [...]
<p>To use a plugin in your unit test class, use the <code>OsgiContextBuilder</code> class instead of directly instantiating the <code>OsgiContext</code>class. This allows you in a fluent style to configure more options, with the <code>plugin(...)</code> method you can add one or more plugins.</p>
<p>Example:</p>
-<pre><code><!-- TODO syntax marker (#!java) disabled -->@Rule
-public OsgiContext context = new OsgiContextBuilder().plugin(MY_PLUGIN).build();
+<pre><code><!-- TODO syntax marker (#!java) disabled -->public OsgiContext context = new OsgiContextBuilder().plugin(MY_PLUGIN).build();
</code></pre>
<p>More examples:</p>
<ul>
@@ -232,7 +261,7 @@ public OsgiContext context = new OsgiContextBuilder().plugin(MY_PLUGIN).build();
</ul></section></div></div>
<div class="footer">
<div class="revisionInfo">
- Last modified by <span class="author">Bertrand Delacretaz</span> on <span class="comment">Fri Sep 29 15:57:01 2017 +0200</span>
+ Last modified by <span class="author">sseifert</span> on <span class="comment">Mon Aug 13 17:16:47 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>
diff --git a/documentation/development/sling-mock.html b/documentation/development/sling-mock.html
index 6394353..90ac1ca 100644
--- a/documentation/development/sling-mock.html
+++ b/documentation/development/sling-mock.html
@@ -99,16 +99,23 @@
<p><!-- TODO reactivate TOC once JBake moves to flexmark-java -->
</p>
<h2><a href="#maven-dependency" id="maven-dependency">Maven Dependency</a></h2>
+<p>For JUnit 4:</p>
<pre><code><!-- TODO syntax marker (#!xml) disabled --><dependency>
<groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.testing.sling-mock</artifactId>
+ <artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
+</dependency>
+</code></pre>
+<p>For JUnit 5:</p>
+<pre><code><!-- TODO syntax marker (#!xml) disabled --><dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId>
</dependency>
</code></pre>
<p>See latest version on the <a href="/downloads.cgi">downloads page</a>.</p>
<p>There are two major version ranges available:</p>
<ul>
- <li>sling-mock 1.x: compatible with older Sling versions from 2014 (Sling API 2.4 and above)</li>
- <li>sling-mock 2.x: compatible with Sling versions from 2016 (Sling API 2.11 and above)</li>
+ <li>sling-mock 1.x: compatible with older Sling versions from 2014 (Sling API 2.4 and above), JUnit 4</li>
+ <li>sling-mock 2.x: compatible with Sling versions from 2016 (Sling API 2.11 and above), JUnit 4 and JUnit 5</li>
</ul>
<h2><a href="#implemented-mock-features" id="implemented-mock-features">Implemented mock features</a></h2>
<p>The mock implementation supports:</p>
@@ -150,23 +157,6 @@
<li><code>ContentBuilder</code> and <code>ResourceBuilder</code> make it easier to create resources and properties as test fixture</li>
</ul>
<h2><a href="#usage" id="usage">Usage</a></h2>
-<h3><a href="#sling-context-junit-rule" id="sling-context-junit-rule">Sling Context JUnit Rule</a></h3>
-<p>The Sling mock context can be injected into a JUnit test using a custom JUnit rule named <code>SlingContext</code>. This rules takes care of all initialization and cleanup tasks required to make sure all unit tests can run independently (and in parallel, if required).</p>
-<p>Example:</p>
-<pre><code><!-- TODO syntax marker (#!java) disabled -->public class ExampleTest {
-
- @Rule
- public final SlingContext context = new SlingContext();
-
- @Test
- public void testSomething() {
- Resource resource = context.resourceResolver().getResource("/content/sample/en");
- // further testing
- }
-
-}
-</code></pre>
-<p>It is possible to combine such a unit test rule with a <code>@RunWith</code> annotation e.g. for <a href="http://mockito.github.io/mockito/docs/current/org/mockito/runners/MockitoJUnitRunner.html">Mockito JUnit Runner</a>.</p>
<p>The <code>SlingContext</code> object provides access to mock implementations of:</p>
<ul>
<li>OSGi Component Context</li>
@@ -182,15 +172,44 @@
<li>Registering adapter factories</li>
<li>Accessing ContentLoader, and ContentBuilder and ResourceBuilder</li>
</ul>
-<h3><a href="#choosing-resource-resolver-mock-type" id="choosing-resource-resolver-mock-type">Choosing Resource Resolver Mock Type</a></h3>
-<p>The Sling mock context supports different resource resolver types. Example:</p>
+<h3><a href="#junit-4-sling-context-junit-rule" id="junit-4-sling-context-junit-rule">JUnit 4: Sling Context JUnit Rule</a></h3>
+<p>The Sling mock context can be injected into a JUnit test using a custom JUnit rule named <code>SlingContext</code>. This rule takes care of all initialization and cleanup tasks required to make sure all unit tests can run independently (and in parallel, if required).</p>
+<p>Example:</p>
<pre><code><!-- TODO syntax marker (#!java) disabled -->public class ExampleTest {
@Rule
- public final SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
+ public final SlingContext context = new SlingContext();
+
+ @Test
+ public void testSomething() {
+ Resource resource = context.resourceResolver().getResource("/content/sample/en");
+ // further testing
+ }
}
</code></pre>
+<p>It is possible to combine such a unit test rule with a <code>@RunWith</code> annotation e.g. for <a href="https://www.javadoc.io/page/org.mockito/mockito-core/latest/org/mockito/junit/MockitoJUnitRunner.html">Mockito JUnit Runner</a>.</p>
+<h3><a href="#junit-5-sling-context-junit-extension" id="junit-5-sling-context-junit-extension">JUnit 5: Sling Context JUnit Extension</a></h3>
+<p>The Sling mock context can be injected into a JUnit test using a custom JUnit extension named <code>SlingContextExtension</code>. This extension takes care of all initialization and cleanup tasks required to make sure all unit tests can run independently (and in parallel, if required).</p>
+<p>Example:</p>
+<pre><code><!-- TODO syntax marker (#!java) disabled -->@ExtendWith(SlingContextExtension.class)
+public class ExampleTest {
+
+ private final SlingContext context = new SlingContext();
+
+ @Test
+ public void testSomething() {
+ Resource resource = context.resourceResolver().getResource("/content/sample/en");
+ // further testing
+ }
+
+}
+</code></pre>
+<p>It is possible to combine such a unit test rule with a <code>@ExtendWith</code> annotation e.g. for <a href="https://www.javadoc.io/page/org.mockito/mockito-junit-jupiter/latest/org/mockito/junit/jupiter/MockitoExtension.html">Mockito JUnit Jupiter Extension</a>.</p>
+<h3><a href="#choosing-resource-resolver-mock-type" id="choosing-resource-resolver-mock-type">Choosing Resource Resolver Mock Type</a></h3>
+<p>The Sling mock context supports different resource resolver types. Example:</p>
+<pre><code><!-- TODO syntax marker (#!java) disabled -->public final SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
+</code></pre>
<p>Different resource resolver mock types are supported with pros and cons, see next chapter for details.</p>
<h3><a href="#resource-resolver-types" id="resource-resolver-types">Resource Resolver Types</a></h3>
<p>The Sling Mocks resource resolver implementation supports different "types" of adapters for the mocks. Depending on the type an underlying JCR repository is used or not, and the data is stored in-memory or in a real repository.</p>
@@ -420,8 +439,7 @@ assertArrayEquals(TEST_DATA, response.getOutput());
<p>To define a plugin implement the <code>org.apache.sling.testing.mock.osgi.context.ContextPlugin<SlingContextImpl></code> interface. For convenience it is recommended to extend the abstract class <code>org.apache.sling.testing.mock.osgi.context.AbstractContextPlugin<SlingContextImpl></code>. These plugins can be used with Sling Mock context, but also with context instances deriving from it like AEM Mocks. In most cases you would just override the <code>afterSetUp</code> met [...]
<p>To use a plugin in your unit test class, use the <code>SlingContextBuilder</code> class instead of directly instantiating the <code>SlingContext</code>class. This allows you in a fluent style to configure more options, with the <code>plugin(...)</code> method you can add one or more plugins.</p>
<p>Example:</p>
-<pre><code><!-- TODO syntax marker (#!java) disabled -->@Rule
-public SlingContext context = new SlingContextBuilder().plugin(MY_PLUGIN).build();
+<pre><code><!-- TODO syntax marker (#!java) disabled -->public SlingContext context = new SlingContextBuilder().plugin(MY_PLUGIN).build();
</code></pre>
<p>More examples:</p>
<ul>
@@ -430,7 +448,7 @@ public SlingContext context = new SlingContextBuilder().plugin(MY_PLUGIN).build(
</ul></section></div></div>
<div class="footer">
<div class="revisionInfo">
- Last modified by <span class="author">Thomas Wolfart</span> on <span class="comment">Wed Jul 18 09:27:08 2018 +0200</span>
+ Last modified by <span class="author">sseifert</span> on <span class="comment">Mon Aug 13 17:16:47 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>