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 2014/11/14 22:56:35 UTC
svn commit: r1639795 - in
/sling/site/trunk/content/documentation/development: osgi-mock.mdtext
sling-mock.mdtext
Author: sseifert
Date: Fri Nov 14 21:56:35 2014
New Revision: 1639795
URL: http://svn.apache.org/r1639795
Log:
update mocks documentation
Modified:
sling/site/trunk/content/documentation/development/osgi-mock.mdtext
sling/site/trunk/content/documentation/development/sling-mock.mdtext
Modified: sling/site/trunk/content/documentation/development/osgi-mock.mdtext
URL: http://svn.apache.org/viewvc/sling/site/trunk/content/documentation/development/osgi-mock.mdtext?rev=1639795&r1=1639794&r2=1639795&view=diff
==============================================================================
--- sling/site/trunk/content/documentation/development/osgi-mock.mdtext (original)
+++ sling/site/trunk/content/documentation/development/osgi-mock.mdtext Fri Nov 14 21:56:35 2014
@@ -21,20 +21,62 @@ See latest version on the [downloads pag
The mock implementation supports:
* Instantiating OSGi `Bundle`, `BundleContext` and `ComponentContext` objects and navigate between them.
-* Read and write properties on them.
-* Register OSGi services and get references to service instances
+* Register OSGi SCR services and get references to service instances
+* Supports reading OSGi SCR metadata from `/OSGI-INF/<pid>.xml`
+* Apply service properties/component configuration provided in unit test and from SCR metadata
+* Inject SCR dependencies
+* Call lifecyle methods for activating, deactivating or modifying SCR components
* Service and bundle listener implementation
-* When adding services to BundleContext OSGi metadata from `/OSGI-INF/<pid>.xml` is read (e.g. for service ranking property)
* Mock implementation of `LogService` which logs to SLF4J in JUnit context
The following features are *not supported*:
-* Activation and deactivation methods of services are not called automatically (but helper methods exist)
-* Dependency injection does not take place automatically (but helper methods exist)
+* Dependencies are only injected and lifecycle methods are only called when initiated by the unit test
## Usage
+### 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
+independently (and in parallel, if required).
+
+Example:
+
+ #!java
+ public class ExampleTest {
+
+ @Rule
+ public final OsgiContext context = new OsgiContext();
+
+ @Test
+ public void testSomething() {
+
+ // register and activate service
+ MyService service1 = context.registerInjectActivateService(new MyService(),
+ ImmutableMap.<String, Object>of("prop1", "value1"));
+
+ // get service instance
+ OtherService service2 = context.getService(OtherService.class);
+
+ }
+
+ }
+
+It is possible to combine such a unit test with a `@RunWith` annotation e.g. for
+[Mockito JUnit Runner][mockito-testrunner].
+
+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
+
+
### Getting OSGi mock objects
The factory class `MockOsgi` allows to instantiate the different mock implementations.
@@ -46,9 +88,8 @@ Example:
BundleContext bundleContext = MockOsgi.newBundleContext();
// get component context
- Dictionary<String,Object> properties = new Hashtable<>();
- properties.put("prop1", "value1");
- BundleContext bundleContext = MockOsgi.newComponentContext(properties);
+ BundleContext bundleContext = MockOsgi.newComponentContext(properties,
+ ImmutableMap.<String, Object>of("prop1", "value1"));
It is possible to simulate registering of OSGi services (backed by a simple hash map internally):
@@ -95,13 +136,5 @@ missing if your clean and build the proj
project again with maven and can run the tests - or use a Maven IDE Integration like m2eclipse.
-### Usage with `SlingContext` JUnit rule
-
-If you are using the `SlingContext` JUnit rule from [Sling Mocks]({{ refs.sling-mock.path }}) it is even easier asseccing and registering OSGi services:
- #!java
- // register service, inject dependencies and call activate method
- context.registerInjectActivateService(myService);
-
- // get service instance
- MyClass service = context.getService(MyClass.class);
+[mockito-testrunner]: http://mockito.github.io/mockito/docs/current/org/mockito/runners/MockitoJUnitRunner.html
Modified: sling/site/trunk/content/documentation/development/sling-mock.mdtext
URL: http://svn.apache.org/viewvc/sling/site/trunk/content/documentation/development/sling-mock.mdtext?rev=1639795&r1=1639794&r2=1639795&view=diff
==============================================================================
--- sling/site/trunk/content/documentation/development/sling-mock.mdtext (original)
+++ sling/site/trunk/content/documentation/development/sling-mock.mdtext Fri Nov 14 21:56:35 2014
@@ -74,7 +74,7 @@ Example:
}
-It is possible to combine such a unit test with a `@RunWith` annotation e.g. for
+It is possible to combine such a unit test rule with a `@RunWith` annotation e.g. for
[Mockito JUnit Runner][mockito-testrunner].
The `SlingContext` object provides access to mock implementations of:
@@ -363,3 +363,4 @@ If you use the `SlingContext` JUnit rule
[resourceresolver-mock]: {{ refs.resourceresolver-mock.path }}
[jcr-resource]: http://svn.apache.org/repos/asf/sling/trunk/bundles/jcr/resource
[sling-comons-testing]: http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/testing
+[mockito-testrunner]: http://mockito.github.io/mockito/docs/current/org/mockito/runners/MockitoJUnitRunner.html