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:17:26 UTC

[sling-site] branch master 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 master
in repository https://gitbox.apache.org/repos/asf/sling-site.git


The following commit(s) were added to refs/heads/master by this push:
     new 583d4fe  SLING-7803 JUnit5 Support for sling-mock and osgi-mock
583d4fe is described below

commit 583d4fe0e0ec4f31faf14c2eee875827279a4739
Author: sseifert <ss...@pro-vision.de>
AuthorDate: Mon Aug 13 17:16:47 2018 +0200

    SLING-7803 JUnit5 Support for sling-mock and osgi-mock
---
 .../content/documentation/development/osgi-mock.md | 69 ++++++++++++++----
 .../documentation/development/sling-mock.md        | 82 +++++++++++++++-------
 2 files changed, 113 insertions(+), 38 deletions(-)

diff --git a/src/main/jbake/content/documentation/development/osgi-mock.md b/src/main/jbake/content/documentation/development/osgi-mock.md
index 87ec787..03204ce 100644
--- a/src/main/jbake/content/documentation/development/osgi-mock.md
+++ b/src/main/jbake/content/documentation/development/osgi-mock.md
@@ -11,18 +11,28 @@ Mock implementation of selected OSGi APIs for easier testing.
 
 ## Maven Dependency
 
+For JUnit 4:
+
     #!xml
     <dependency>
       <groupId>org.apache.sling</groupId>
-      <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
+      <artifactId>org.apache.sling.testing.osgi-mock.junit4</artifactId>
+    </dependency>
+
+For JUnit 5:
+
+    #!xml
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.testing.osgi-mock.junit5</artifactId>
     </dependency>
 
 See latest version on the [downloads page](/downloads.cgi).
 
 There are two major version ranges available:
 
-* osgi-mock 1.x: compatible with OSGi R4 and above
-* osgi-mock 2.x: compatible with OSGi R6 and above
+* osgi-mock 1.x: compatible with OSGi R4 and above, JUnit 4
+* osgi-mock 2.x: compatible with OSGi R6 and above, JUnit 4 and JUnit 5
 
 
 ## Implemented mock features
@@ -48,10 +58,20 @@ Since osgi-mock 2.0.0:
 
 ## Usage
 
-### OSGi Context JUnit Rule
+The `OsgiContext` object provides access to mock implementations of:
+
+* OSGi Component Context
+* OSGi Bundle Context
+
+Additionally it supports:
+
+* Registering and activating OSGi services and inject dependencies
+
+
+### JUnit 4: OSGi Context JUnit Rule
 
 The OSGi mock context can be injected into a JUnit test using a custom JUnit rule named `OsgiContext`.
-This rules takes care of all initialization and cleanup tasks required to make sure all unit tests can run 
+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).
 
 Example:
@@ -77,16 +97,39 @@ Example:
     }
 
 It is possible to combine such a unit test with a `@RunWith` annotation e.g. for
-[Mockito JUnit Runner][mockito-testrunner].
+[Mockito JUnit Runner][mockito-junit4-testrunner].
 
-The `OsgiContext` object provides access to mock implementations of:
 
-* OSGi Component Context
-* OSGi Bundle Context
+### JUnit 5: OSGi Context JUnit Extension
 
-Additionally it supports:
+The OSGi mock context can be injected into a JUnit test using a custom JUnit extension named `OsgiContextExtension`.
+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).
 
-* Registering and activating OSGi services and inject dependencies
+Example:
+
+    #!java
+    @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);
+
+      }
+
+    }
+
+It is possible to combine such a unit test with a `@ExtendWith` annotation e.g. for
+[Mockito JUnit Jupiter Extension][mockito-junit5-extension].
 
 
 ### Getting OSGi mock objects
@@ -175,7 +218,6 @@ To use a plugin in your unit test class, use the `OsgiContextBuilder` class inst
 Example: 
 
     #!java
-    @Rule
     public OsgiContext context = new OsgiContextBuilder().plugin(MY_PLUGIN).build();
 
 More examples:
@@ -185,6 +227,7 @@ More examples:
 
 
 
-[mockito-testrunner]: http://mockito.github.io/mockito/docs/current/org/mockito/runners/MockitoJUnitRunner.html
+[mockito-junit4-testrunner]: https://www.javadoc.io/page/org.mockito/mockito-core/latest/org/mockito/junit/MockitoJUnitRunner.html
+[mockito-junit5-extension]: https://www.javadoc.io/page/org.mockito/mockito-junit-jupiter/latest/org/mockito/junit/jupiter/MockitoExtension.html
 [caconfig-mock-plugin]: https://github.com/apache/sling/blob/trunk/contrib/extensions/contextaware-config/testing/mocks/caconfig-mock-plugin/src/main/java/org/apache/sling/testing/mock/caconfig/ContextPlugins.java
 [caconfig-mock-plugin-test]: https://github.com/apache/sling/blob/trunk/contrib/extensions/contextaware-config/testing/mocks/caconfig-mock-plugin/src/test/java/org/apache/sling/testing/mock/caconfig/ContextPluginsTest.java
diff --git a/src/main/jbake/content/documentation/development/sling-mock.md b/src/main/jbake/content/documentation/development/sling-mock.md
index 696430f..cf73904 100644
--- a/src/main/jbake/content/documentation/development/sling-mock.md
+++ b/src/main/jbake/content/documentation/development/sling-mock.md
@@ -11,10 +11,20 @@ Mock implementation of selected Sling APIs for easier testing.
 
 ## Maven Dependency
 
+For JUnit 4:
+
+    #!xml
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
+    </dependency>
+
+For JUnit 5:
+
     #!xml
     <dependency>
       <groupId>org.apache.sling</groupId>
-      <artifactId>org.apache.sling.testing.sling-mock</artifactId>
+      <artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId>
     </dependency>
 
 See latest version on the [downloads page](/downloads.cgi).
@@ -22,8 +32,8 @@ See latest version on the [downloads page](/downloads.cgi).
 
 There are two major version ranges available:
 
-* sling-mock 1.x: compatible with older Sling versions from 2014 (Sling API 2.4 and above)
-* sling-mock 2.x: compatible with Sling versions from 2016 (Sling API 2.11 and above)
+* sling-mock 1.x: compatible with older Sling versions from 2014 (Sling API 2.4 and above), JUnit 4
+* sling-mock 2.x: compatible with Sling versions from 2016 (Sling API 2.11 and above), JUnit 4 and JUnit 5
 
 
 ## Implemented mock features
@@ -64,10 +74,26 @@ Additional features provided:
 
 ## Usage
 
-### Sling Context JUnit Rule
+The `SlingContext` object provides access to mock implementations of:
+
+* OSGi Component Context
+* OSGi Bundle Context
+* Sling Resource Resolver
+* Sling Request
+* Sling Response
+* Sling Script Helper
+
+Additionally it supports:
+
+* Registering OSGi services
+* Registering adapter factories
+* Accessing ContentLoader, and ContentBuilder and ResourceBuilder
+
+
+### JUnit 4: Sling Context JUnit Rule
 
 The Sling mock context can be injected into a JUnit test using a custom JUnit rule named `SlingContext`.
-This rules takes care of all initialization and cleanup tasks required to make sure all unit tests can run 
+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).
 
 Example:
@@ -87,22 +113,33 @@ Example:
     }
 
 It is possible to combine such a unit test rule with a `@RunWith` annotation e.g. for
-[Mockito JUnit Runner][mockito-testrunner].
+[Mockito JUnit Runner][mockito-junit4-testrunner].
 
-The `SlingContext` object provides access to mock implementations of:
 
-* OSGi Component Context
-* OSGi Bundle Context
-* Sling Resource Resolver
-* Sling Request
-* Sling Response
-* Sling Script Helper
+### JUnit 5: Sling Context JUnit Extension
 
-Additionally it supports:
+The Sling mock context can be injected into a JUnit test using a custom JUnit extension named `SlingContextExtension`.
+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).
 
-* Registering OSGi services
-* Registering adapter factories
-* Accessing ContentLoader, and ContentBuilder and ResourceBuilder
+Example:
+
+    #!java
+    @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
+      }
+
+    }
+
+It is possible to combine such a unit test rule with a `@ExtendWith` annotation e.g. for
+[Mockito JUnit Jupiter Extension][mockito-junit5-extension].
 
 
 ### Choosing Resource Resolver Mock Type
@@ -110,12 +147,7 @@ Additionally it supports:
 The Sling mock context supports different resource resolver types. Example:
 
     #!java
-    public class ExampleTest {
-
-      @Rule
-      public final SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
-
-    }
+    public final SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
 
 Different resource resolver mock types are supported with pros and cons, see next chapter for details.
 
@@ -431,7 +463,6 @@ To use a plugin in your unit test class, use the `SlingContextBuilder` class ins
 Example: 
 
     #!java
-    @Rule
     public SlingContext context = new SlingContextBuilder().plugin(MY_PLUGIN).build();
 
 More examples:
@@ -444,6 +475,7 @@ More examples:
 [resourceresolver-mock]: /documentation/development/resourceresolver-mock.html
 [jcr-resource]: https://github.com/apache/sling-org-apache-sling-jcr-resource
 [sling-commons-testing]: https://github.com/apache/sling-org-apache-sling-commons-testing
-[mockito-testrunner]: http://mockito.github.io/mockito/docs/current/org/mockito/runners/MockitoJUnitRunner.html
+[mockito-junit4-testrunner]: https://www.javadoc.io/page/org.mockito/mockito-core/latest/org/mockito/junit/MockitoJUnitRunner.html
+[mockito-junit5-extension]: https://www.javadoc.io/page/org.mockito/mockito-junit-jupiter/latest/org/mockito/junit/jupiter/MockitoExtension.html
 [caconfig-mock-plugin]: https://github.com/apache/sling-org-apache-sling-testing-caconfig-mock-plugin/blob/master/src/main/java/org/apache/sling/testing/mock/caconfig/ContextPlugins.java
 [caconfig-mock-plugin-test]: https://github.com/apache/sling-org-apache-sling-testing-caconfig-mock-plugin/blob/master/src/test/java/org/apache/sling/testing/mock/caconfig/ContextPluginsTest.java