Java Reference
In-Depth Information
Why wait until the end of a project to discover if your code works in the strict envi-
ronment of an
OSG
i framework or how well your chosen bundles fit together? Migrate
and modularize your tests along with your code! This chapter will help put this advice
into practice by taking you through three different approaches:
Running existing tests on
OSG
i
■
Mocking out calls to
OSG
i
API
s
■
Advanced
OSG
i testing
■
The last section in particular takes a closer look at how unit and integration test con-
cepts relate to modular applications and introduces the idea of management testing.
If you're eager to learn more about testing modularity and you're already familiar
with in-container tests and object mocking, feel free to skip ahead to section 7.3.
By the end of this chapter, you should be comfortable with testing
OSG
i applica-
tions, which will lead to better quality bundles for everyone. Let's start by continuing
the theme from chapter 6 and get some existing tests running on an
OSG
i framework.
7.1
Migrating tests to OSGi
Imagine you have an application that you want to modularize and move to
OSG
i. You
almost certainly have existing tests that check requirements and expected behavior.
You can use these tests to verify and validate the modularization process, either by
manually running them at key stages or by using an automated build system that runs
tests on a regular schedule—say, whenever people check in code. These tests give you
confidence that your modularized application is to some extent equivalent to the orig-
inal, at least when run with the test framework. But what they don't tell you is whether
your code behaves the same inside an
OSG
i container.
To find out, you need to run your tests
twice
: inside the target container as well as
outside. Running these tests outside the container is a matter of using your favorite
testng.org/
). There are many good topics on testing standard Java applications, so we
assume you already know how to write unit tests and run them using Ant, Maven, or
your
IDE
. But what about testing inside an
OSG
i container; how does it work in prac-
tice, and is it worth the effort?
7.1.1
In-container testing
Would you develop and deploy a web application without ever testing it inside an
application server? Would you ship a product without testing its installer? Of course
not! It's important to test code in the right environment. If you expect to use a class
with
OSG
i, you should test it inside an
OSG
i framework—how else will you discover
potential class-loading or visibility issues? But before you can run your existing
JU
nit
or Test
NG
tests inside the container, you first need to deploy them.
As you saw in chapter 6, whenever you want to deploy something into an
OSG
i frame-
work, you must consider packaging and placement. If the test classes are (accidentally)