Java Reference
In-Depth Information
We start by creating a new JU nit test case B that will exercise our CalculatorService .
Just as we did in chapter 6, we define the Mockery context C and the objects that we
want to mock D . The @Before method executes before every test method, so that's
where we create the mock objects E . At F , we define the expectation for the mock
objects. We got lucky—these expectations turn out to be the same for both tests,
which is why we can extract them to the @Before method. We first call the get-
ServiceReference on the mockBundleContext , and after that we call the getService
on the same object, passing the ServiceReference we already have. Finally, we call the
ungetService method on the mockBundleContext .
We have two tests: one to test the add method of the service G and another to test
the multiply method . Those tests have the same structure: We first get a new
instance of the ClientBundleActivator class H and then set the parameters using
the setter methods on the class I . The last step is to invoke the start method with
our fake object J and assert that the expected results are good.
As you can see, using mock objects to test our service requires quite a bit of prepa-
ration. We need to take care of the proper instantiation and configuration of the
mock objects.
The next section introduces the JU nit4 OSG i project, which implements an in-
container strategy for testing the CalculatorClient service.
1)
16.4
Introducing JUnit4OSGi
The JU nit4 OSG i framework is a simple OSG i testing framework that comes from the
i POJO subcomponent of the Apache Felix project. You can download the JAR s from
the website of the project, or if you're using Maven, you can declare them as depen-
dencies in your Maven project.
To use the framework you'll need several JAR files. The bare minimum you need
is this:
org.apache.felix.ipojo-1.4.0.jar
org.apache.felix.ipojo.handler.extender-1.4.0.jar
org.apache.felix.ipojo.junit4osgi-1.0.0.jar
org.apache.felix.ipojo.junit4osgi.felix-command-1.0.0.jar
Place these files in the sample folder FELIX_HOME /bundles/junit4osgi. Then install
them one by one with the Felix command-line tool. Once they're installed and
started, you'll get the chance to use one extra command on the command line:
-> junit [BundleID]
This command will invoke all the JU nit and JU nit4 OSG i tests that are present in the
bundle with the given [BundleID] and are listed in the manifest descriptor.
Let's start implementing our first test cases. Listing 16.9 shows the JU nit4 OSG i test
case that tests the CalculatorService application.
 
 
 
 
Search WWH ::




Custom Search