blob: df4a3bc94b144d77df966285e6d5dfea8962c1a1 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE- 2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
<title>Apache Felix - Junit4OSGi</title>
<link rel="icon" href="/res/favicon.ico">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="The most powerful component model for OSGi">
<link href="/ipojo/web/bootstrap/css/bootstrap-cerulean.css" rel="stylesheet">
<link href="/ipojo/web/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<link href="/ipojo/web/bootstrap/css/font-awesome.min.css" rel="stylesheet">
<link href="/ipojo/web/style.css" rel="stylesheet">
<!-- Overide alert's colors -->
<link href="/ipojo/web/bootstrap/css/alert.css" rel="stylesheet">
<link rel="stylesheet" href="/ipojo/web/github.css" type="text/css" media="all">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="/ipojo/web/bootstrap/js/bootstrap.min.js"></script>
</head>
<body data-spy="scroll" data-target=".subnav">
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="/documentation/subprojects/apache-felix-ipojo.html">Apache Felix iPOJO</a>
<div class="nav-collapse" id="main-menu">
<ul class="nav" id="main-menu-left">
<li><a href="/documentation/subprojects/apache-felix-ipojo/ipojo-news.html">News</a></li>
<li><a href="http://felix.apache.org/downloads.cgi">Downloads</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Tutorials <b class="caret"></b></a>
<ul class="dropdown-menu" id="tutorials-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-why-choose-ipojo.html">Why choose iPOJO</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-successstories.html">Success stories</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-feature-overview.html">Features</a></li>
<li class="divider"></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-in-10-minutes.html">iPOJO in 10 minutes</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/how-to-use-ipojo-annotations.html">Using Annotations</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-hello-word-maven-based-tutorial.html">Maven tutorial</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-advanced-tutorial.html">Advanced tutorial</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/apache-felix-ipojo-dosgi.html">Using Distributed OSGi</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-gettingstarted/ipojo-composition-tutorial.html">Application Composition</a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Documentation <b class="caret"></b></a>
<ul class="dropdown-menu" id="user-guide-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/service-requirement-handler.html">Requiring a service</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/providing-osgi-services.html">Providing a service</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/lifecycle-callback-handler.html">Lifecycle management</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/configuration-handler.html">Configuration</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/architecture-handler.html">Introspection</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/controller-lifecycle-handler.html">Impacting the lifecycle</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/injecting-bundle-context.html">Accessing the Bundle Context</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/apache-felix-ipojo-instances.html">Creating instances</a></li>
<li class="divider"></li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">External <em>handlers</em></a>
<ul class="dropdown-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/event-admin-handlers.html">Asynchronous communication</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/ipojo-jmx-handler.html">JMX management</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/extender-pattern-handler.html">Extender pattern</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/white-board-pattern-handler.html">Whiteboard pattern</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/temporal-service-dependency.html">Temporal dependencies</a></li>
</ul>
</li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Configuration Admin &amp; Factories</a>
<ul class="dropdown-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/combining-ipojo-and-configuration-admin.html">iPOJO and config admin</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/ipojo-factory-service.html">Using the iPOJO Factory service</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/how-to-use-ipojo-factories.html">Factories and Instances</a></li>
</ul>
</li>
<li class="divider"></li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Advanced topics</a>
<ul class="dropdown-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/instance-vs-service-controller.html">Instance vs. Service Controllers</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/service-binding-interceptors.html">Service Binding Interceptors</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/using-xml-schemas.html">XML Schemas</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/apache-felix-ipojo-api.html">Using the iPOJO API</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/constructing-pojo-objects-with-factory-methods.html">Constructing service objects with factory methods</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/using-ipojo-introspection-api.html">Using the introspection API</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-testing-components.html">Testing components</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/using-stereotypes.html">Using @Stereotypes</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-eclipse-integration.html">Eclipse Integration</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/ipojo-extender-configuration.html">Configuring iPOJO's Extender</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-faq.html">FAQ</a></li>
<li class="divider"></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/how-to-write-your-own-handler.html">Handler development</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/how-to-use-ipojo-manipulation-metadata.html">Manipulation Metadata </a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-devguide/dive-into-the-ipojo-manipulation-depths.html">Dive into the iPOJO Manipulation depths</a></li>
<li><a href="http://felix.apache.org/ipojo/api/1.12.1">Javadoc</a></li>
</ul>
</li>
</ul>
</li>
<li class="dropdown" id="tools-menu">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Tools <b class="caret"></b></a>
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-ant-task.html">Ant Task</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-maven-plug-in.html">Maven Plugin</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-arch-command.html">Architecture commands</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/apache-felix-ipojo-online-manipulator.html">Online Manipulator</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-webconsole-plugin.html">Webconsole plugin</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-karaf-feature.html">Apache Karaf Features</a></li>
</ul>
</li>
<li class="dropdown" id="community-menu">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Community <b class="caret"></b></a>
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/ipojo-support.html">Support</a></li>
<li><a href="http://www.apache.org/">ASF</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html">Sponsors</a></li>
</ul>
</li>
<li class="dropdown" id="misc-menu">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Misc <b class="caret"></b></a>
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-supportedvms.html">Supported JVMs</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-supportedosgi.html">Supported OSGi Implementations</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/articles-and-presentations.html">Article & Presentations</a></li>
<li><a href="/documentation/subprojects/apache-felix-ipojo/developing-camel-mediators-with-ipojo.html">Developping Camel mediators with iPOJO</a></li>
</ul>
</li>
</ul>
<ul class="nav pull-right" id="main-menu-right">
<li><a rel="tooltip" target="_blank" href="http://felix.apache.org">Apache Felix <i class="icon-share-alt"></i></a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container">
<div class="content">
<style type="text/css">
/* The following code is added by mdx_elementid.py
It was originally lifted from http://subversion.apache.org/style/site.css */
/*
* Hide class="elementid-permalink", except when an enclosing heading
* has the :hover property.
*/
.headerlink, .elementid-permalink {
visibility: hidden;
}
h2:hover > .headerlink, h3:hover > .headerlink, h1:hover > .headerlink, h6:hover > .headerlink, h4:hover > .headerlink, h5:hover > .headerlink, dt:hover > .elementid-permalink { visibility: visible }</style>
<h1 id="junit4osgi-executing-unitary-and-integration-test-on-osgi">JUnit4OSGi : Executing Unitary and Integration Test on OSGi<a class="headerlink" href="#junit4osgi-executing-unitary-and-integration-test-on-osgi" title="Permanent link">&para;</a></h1>
<p>This page is <em>deprecated</em></p>
<hr />
<h2 id="goals">Goals<a class="headerlink" href="#goals" title="Permanent link">&para;</a></h2>
<p>Executing tests on OSGi became a stringent requirement as several aspects need to be tested. Indeed, modularity, services (and the inherent dynamism) and the business logic need to be tested. The goal of JUnit4OSGi is to provide an execution gateway in order to test bundles and services. Instead of injecting mock objects, as provided by regular test frameworks to hide the underlying Executing environment, tests are directly executed on OSGi and so have access to all the features provided by the OSGi framework. This allows developers checking modularity aspect and service dynamism in addition to the business logic.</p>
<h2 id="principles">Principles<a class="headerlink" href="#principles" title="Permanent link">&para;</a></h2>
<p>Junit4OSGi is split in several pieces. First the core bundle (Junit4OSGi) contains an extended version of Junit (3.8.1). On the top of this framework, several runners are implemented:
<em> Immediate runner : launch every available test suite
</em> Command line runner : Felix command line allowing to choose the test suite(s) to launch
<em> Swing Runner : a very simple Swing runner
</em> Maven runner: a maven plug-in allowing to automate tests during the project build process</p>
<p>The different runners give a great flexibility to the framework that can be used during the build process or to test an application in a special environment (such as J2ME).
Test suites and test cases are package inside bundles. Junit4OSGi extend Junit to support OSGi test suite and OSGi test case. These two kinds of test classes are able to use the bundle context of the bundle and so are able to deal with the underlying OSGi framework.</p>
<h2 id="installing-the-framework">Installing the framework<a class="headerlink" href="#installing-the-framework" title="Permanent link">&para;</a></h2>
<p>Except if you use the maven front end (automating the launch a an OSGi framework (Felix) and deploying required bundles), to launch the Junit framework you need to deploy and start
<em> <a href="http://people.apache.org/~clement/ipojo/binaries/org.apache.felix.ipojo-0.8.0-SNAPSHOT.jar">iPOJO 0.8.0-SNASPHOT</a>
</em> <a href="http://people.apache.org/~clement/ipojo/binaries/org.apache.felix.ipojo.handler.extender.pattern-0.8.0-SNAPSHOT.jar">iPOJO Extender Pattern Handler 0.8.0-SNAPSHOT</a>
<em> <a href="http://people.apache.org/~clement/ipojo/junit4osgi/org.apache.felix.ipojo.junit4osgi-0.8.0-SNAPSHOT.jar">The Junit4OSGi bundle</a>
</em> One runner such as the <a href="http://people.apache.org/~clement/ipojo/junit4osgi/org.apache.felix.ipojo.junit4osgi.felix-command-0.8.0-SNAPSHOT.jar">Felix command</a> one</p>
<p>The archive available <a href="http://people.apache.org/~clement/ipojo/junit4osgi/junit4osgi.tutorial.zip">here</a> contains a pre-configured version of Felix with the previously mentionned bundles.</p>
<h2 id="writing-junit-test-case-and-test-suite">Writing Junit Test Case and Test Suite<a class="headerlink" href="#writing-junit-test-case-and-test-suite" title="Permanent link">&para;</a></h2>
<p>This section describes briefly how to write test cases and test suites and how they are used by the Junit4OSGi framework. More details are available on the Junit web site <a href="http://www.junit.org/">http://www.junit.org/</a>.</p>
<p>The Money class was introduced in the Junit tutorial and was implemented as the following:</p>
<div class="codehilite"><pre><span class="n">package</span> <span class="n">junit</span><span class="p">.</span><span class="n">money</span><span class="p">;</span>
<span class="n">public</span> <span class="n">class</span> <span class="n">Money</span> <span class="p">{</span>
<span class="n">private</span> <span class="n">int</span> <span class="n">fAmount</span><span class="p">;</span>
<span class="n">private</span> <span class="n">String</span> <span class="n">fCurrency</span><span class="p">;</span>
<span class="n">public</span> <span class="n">Money</span><span class="p">(</span><span class="n">int</span> <span class="n">amount</span><span class="p">,</span> <span class="n">String</span> <span class="n">currency</span><span class="p">)</span> <span class="p">{</span>
<span class="n">fAmount</span><span class="p">=</span> <span class="n">amount</span><span class="p">;</span>
<span class="n">fCurrency</span><span class="p">=</span> <span class="n">currency</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">int</span> <span class="n">amount</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">fAmount</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">String</span> <span class="n">currency</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">fCurrency</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">Money</span> <span class="n">add</span><span class="p">(</span><span class="n">Money</span> <span class="n">m</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">new</span> <span class="n">Money</span><span class="p">(</span><span class="n">amount</span><span class="p">()</span><span class="o">+</span><span class="n">m</span><span class="p">.</span><span class="n">amount</span><span class="p">(),</span> <span class="n">currency</span><span class="p">());</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">boolean</span> <span class="n">equals</span><span class="p">(</span><span class="n">Object</span> <span class="n">anObject</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">anObject</span> <span class="n">instanceof</span> <span class="n">Money</span><span class="p">)</span> <span class="p">{</span>
<span class="n">Money</span> <span class="n">aMoney</span><span class="p">=</span> <span class="p">(</span><span class="n">Money</span><span class="p">)</span><span class="n">anObject</span><span class="p">;</span>
<span class="k">return</span> <span class="n">aMoney</span><span class="p">.</span><span class="n">currency</span><span class="p">().</span><span class="n">equals</span><span class="p">(</span><span class="n">currency</span><span class="p">())</span>
<span class="o">&amp;&amp;</span> <span class="n">amount</span><span class="p">()</span> <span class="o">==</span> <span class="n">aMoney</span><span class="p">.</span><span class="n">amount</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">false</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>So, to test this implementation, we decide to write a simple class testing the equals and add methods. This class can be implemented as the following:</p>
<div class="codehilite"><pre><span class="n">public</span> <span class="n">class</span> <span class="n">SimpleTestCase</span> <span class="n">extends</span> <span class="n">TestCase</span> <span class="p">{</span>
<span class="n">private</span> <span class="n">Money</span> <span class="n">f12CHF</span><span class="p">;</span>
<span class="n">private</span> <span class="n">Money</span> <span class="n">f14CHF</span><span class="p">;</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">setUp</span><span class="p">()</span> <span class="p">{</span>
<span class="n">f12CHF</span><span class="p">=</span> <span class="n">new</span> <span class="n">Money</span><span class="p">(</span>12<span class="p">,</span> &quot;<span class="n">CHF</span>&quot;<span class="p">);</span>
<span class="n">f14CHF</span><span class="p">=</span> <span class="n">new</span> <span class="n">Money</span><span class="p">(</span>14<span class="p">,</span> &quot;<span class="n">CHF</span>&quot;<span class="p">);</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">testEquals</span><span class="p">()</span> <span class="p">{</span>
<span class="n">assertTrue</span><span class="p">(</span>!<span class="n">f12CHF</span><span class="p">.</span><span class="n">equals</span><span class="p">(</span><span class="n">null</span><span class="p">));</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">f12CHF</span><span class="p">,</span> <span class="n">f12CHF</span><span class="p">);</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">f12CHF</span><span class="p">,</span> <span class="n">new</span> <span class="n">Money</span><span class="p">(</span>12<span class="p">,</span> &quot;<span class="n">CHF</span>&quot;<span class="p">));</span>
<span class="n">assertTrue</span><span class="p">(</span>!<span class="n">f12CHF</span><span class="p">.</span><span class="n">equals</span><span class="p">(</span><span class="n">f14CHF</span><span class="p">));</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">testSimpleAdd</span><span class="p">()</span> <span class="p">{</span>
<span class="n">Money</span> <span class="n">expected</span><span class="p">=</span> <span class="n">new</span> <span class="n">Money</span><span class="p">(</span>26<span class="p">,</span> &quot;<span class="n">CHF</span>&quot;<span class="p">);</span>
<span class="n">Money</span> <span class="n">result</span><span class="p">=</span> <span class="n">f12CHF</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">f14CHF</span><span class="p">);</span>
<span class="n">assertTrue</span><span class="p">(</span><span class="n">expected</span><span class="p">.</span><span class="n">equals</span><span class="p">(</span><span class="n">result</span><span class="p">));</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>Moreover, in order to launch these tests, we create a test suite (declaring the list of test to execute):</p>
<div class="codehilite"><pre><span class="n">public</span> <span class="n">class</span> <span class="n">SimpleTestSuite</span> <span class="p">{</span>
<span class="n">public</span> <span class="n">static</span> <span class="n">Test</span> <span class="n">suite</span><span class="p">()</span> <span class="p">{</span>
<span class="n">TestSuite</span> <span class="n">suite</span> <span class="p">=</span> <span class="n">new</span> <span class="n">TestSuite</span><span class="p">(</span>&quot;<span class="n">Money</span> <span class="n">Simple</span> <span class="n">Test</span> <span class="n">Suite</span>&quot;<span class="p">);</span>
<span class="n">suite</span><span class="p">.</span><span class="n">addTestSuite</span><span class="p">(</span><span class="n">SimpleTestCase</span><span class="p">.</span><span class="n">class</span><span class="p">);</span>
<span class="k">return</span> <span class="n">suite</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>Now, we have to package our tests and the Money class inside a bundle. Of course, it should be packaged in two different bundles but to keep simple we choose to create just one. To create the bundle, launch "ant" at the root of the junit-example directory.
The resulting bundle contains our three classes. These tests are connected to the junit4osgi framework by specifying a special header in the manifest:</p>
<div class="codehilite"><pre><span class="n">Test</span><span class="o">-</span><span class="n">Suite</span><span class="p">:</span> <span class="n">junit</span><span class="p">.</span><span class="n">example</span><span class="p">.</span><span class="n">SimpleTestSuite</span>
</pre></div>
<p>This header lists the test suite contained in the bundle.
Once packaged, we can launch our tests.
Start the pre-configured Felix with the following command launched from the Felix directory:</p>
<div class="codehilite"><pre><span class="n">java</span> <span class="o">-</span><span class="n">jar</span> <span class="n">bin</span><span class="o">/</span><span class="n">felix</span><span class="p">.</span><span class="n">jar</span>
</pre></div>
<p>Then deploy the created bundle:</p>
<div class="codehilite"><pre><span class="n">start</span> <span class="n">file</span><span class="p">:.</span><span class="o">./</span><span class="n">junit</span><span class="o">-</span><span class="n">example</span><span class="o">/</span><span class="n">output</span><span class="o">/</span><span class="n">junit</span><span class="o">-</span><span class="n">example</span><span class="p">.</span><span class="n">jar</span>
</pre></div>
<p>Finally, run the test with the junit command</p>
<div class="codehilite"><pre><span class="n">junit</span> 8
</pre></div>
<p>The argument indicates the bundle containing the test suite to execute. The "all" special argument runs every available test suites.</p>
<div class="codehilite"><pre><span class="o">-&gt;</span> <span class="n">junit</span> 8
<span class="n">Executing</span> <span class="p">[</span><span class="n">Money</span> <span class="n">Simple</span> <span class="n">Test</span> <span class="n">Suite</span><span class="p">]</span>
<span class="p">..</span>
<span class="n">Time</span><span class="p">:</span> 0<span class="p">,</span>001
<span class="n">OK</span> <span class="p">(</span>2 <span class="n">tests</span><span class="p">)</span>
</pre></div>
<h2 id="writing-junit-test-case-and-test-suite-specialized-for-osgi">Writing Junit Test Case and Test Suite specialized for OSGi<a class="headerlink" href="#writing-junit-test-case-and-test-suite-specialized-for-osgi" title="Permanent link">&para;</a></h2>
<p>The previous example has illustrated how creating thest cases and test suites by using the regular Junit framework. However, junit4osgi has the particularity to support specialized test for OSGi. For example, imagine the very simple Hello Service implemented by the following class:</p>
<div class="codehilite"><pre><span class="n">public</span> <span class="n">class</span> <span class="n">HelloProvider</span> <span class="n">implements</span> <span class="n">HelloService</span> <span class="p">{</span>
<span class="n">public</span> <span class="n">String</span> <span class="n">getHelloMessage</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> &quot;<span class="n">hello</span>&quot;<span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>The bundle containing this implementation should register it. So you can decide to check this and to check the returned hello message.
Instead of extending TestCase, this test case will extend OSGiTestCase (provided by the Junit4OSGi framework):</p>
<div class="codehilite"><pre><span class="n">public</span> <span class="n">class</span> <span class="n">SimpleTestCase</span> <span class="n">extends</span> <span class="n">OSGiTestCase</span> <span class="p">{</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">testHelloAvailability</span><span class="p">()</span> <span class="p">{</span>
<span class="n">ServiceReference</span> <span class="n">ref</span> <span class="p">=</span> <span class="n">context</span><span class="p">.</span><span class="n">getServiceReference</span><span class="p">(</span><span class="n">HelloService</span><span class="p">.</span><span class="n">class</span><span class="p">.</span><span class="n">getName</span><span class="p">());</span>
<span class="n">assertNotNull</span><span class="p">(</span>&quot;<span class="n">Assert</span> <span class="n">Availability</span>&quot;<span class="p">,</span> <span class="n">ref</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">public</span> <span class="n">void</span> <span class="n">testHelloMessage</span><span class="p">()</span> <span class="p">{</span>
<span class="n">ServiceReference</span> <span class="n">ref</span> <span class="p">=</span>
<span class="n">context</span><span class="p">.</span><span class="n">getServiceReference</span><span class="p">(</span><span class="n">HelloService</span><span class="p">.</span><span class="n">class</span><span class="p">.</span><span class="n">getName</span><span class="p">());</span>
<span class="n">assertNotNull</span><span class="p">(</span>&quot;<span class="n">Assert</span> <span class="n">Availability</span>&quot;<span class="p">,</span> <span class="n">ref</span><span class="p">);</span>
<span class="n">HelloService</span> <span class="n">hs</span> <span class="p">=</span> <span class="p">(</span><span class="n">HelloService</span><span class="p">)</span> <span class="n">context</span><span class="p">.</span><span class="n">getService</span><span class="p">(</span><span class="n">ref</span><span class="p">);</span>
<span class="n">String</span> <span class="n">message</span> <span class="p">=</span> <span class="n">hs</span><span class="p">.</span><span class="n">getHelloMessage</span><span class="p">();</span>
<span class="n">assertNotNull</span><span class="p">(</span>&quot;<span class="n">Check</span> <span class="n">the</span> <span class="n">message</span> <span class="n">existence</span>&quot;<span class="p">,</span> <span class="n">message</span><span class="p">);</span>
<span class="n">assertEquals</span><span class="p">(</span>&quot;<span class="n">Check</span> <span class="n">the</span> <span class="n">message</span>&quot;<span class="p">,</span> &quot;<span class="n">hello</span>&quot;<span class="p">,</span> <span class="n">message</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>First, note that this class follows the same rules than regular test cases. All methods with a name starting with <em>test</em> will be executed. However, this class can access to the bundle context of its bundle by using the <em>context</em> field. By using this field, the class can check the availability of services ...
OSGi test cases are gathered in OSGi test suite such as:</p>
<div class="codehilite"><pre><span class="n">public</span> <span class="n">class</span> <span class="n">SimpleTestSuite</span> <span class="p">{</span>
<span class="n">public</span> <span class="n">static</span> <span class="n">Test</span> <span class="n">suite</span><span class="p">(</span><span class="n">BundleContext</span> <span class="n">context</span><span class="p">)</span> <span class="p">{</span>
<span class="n">OSGiTestSuite</span> <span class="n">suite</span> <span class="p">=</span> <span class="n">new</span> <span class="n">OSGiTestSuite</span><span class="p">(</span>
&quot;<span class="n">Hello</span> <span class="n">Service</span> <span class="n">Test</span> <span class="n">Suite</span>&quot;<span class="p">,</span>
<span class="n">context</span>
<span class="p">);</span>
<span class="n">suite</span><span class="p">.</span><span class="n">addTestSuite</span><span class="p">(</span><span class="n">SimpleTestCase</span><span class="p">.</span><span class="n">class</span><span class="p">);</span>
<span class="k">return</span> <span class="n">suite</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>Note that the suite method receives the bundle context allowing the creation of an OSGi Test Suite object.
The execution of these tests follows the same steps than the previous example:
<em> Compile the tests and create the bundle by launching ant from the junit-osgi-example directory
</em> Start Felix
<em> Deploy the bundle with the following command: <code>start file:../junit-osgi-example/output/junit-osgi-example.jar</code>
</em> Execute the test by using the junit command</p>
<h2 id="maven-front-end-automating-tests-in-your-build-process">Maven front end: automating tests in your build process<a class="headerlink" href="#maven-front-end-automating-tests-in-your-build-process" title="Permanent link">&para;</a></h2>
<p><em>Coming soon</em></p>
</div>
</div>
<hr/>
<div class="container">
<footer id="footer">
<div class="row">
<div class="trademarkFooter span7">
Apache Felix, Felix, Apache, the Apache feather logo, and the Apache Felix project
logo are trademarks of The Apache Software Foundation. All other marks mentioned
may be trademarks or registered trademarks of their respective owners.
</div>
<div class="timestamp span3 offset2">
Rev. 1700393 by cziegeler on Tue, 1 Sep 2015 06:04:06 +0000
</div>
</div>
</footer>
</div>
</body>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-1518442-4");
pageTracker._trackPageview();
} catch(err) {}
</script>
</html>