Chapter 4. Integration Testing with Spring-DM


In this chapter we are going to create another project which will use Testing framework provided by Spring DM on top of JUnit Test framework to test bundles without deploying them into OSGi environment. For more details on Spring DM Testing framework please refer to Chapter 9. Testing OSGi based Applications or Spring-DM reference manual.

4.1. Develop the Integration Test

Create Test Project

Our test project is typical Eclipse Java project therefore details on how to create it are skipped. 

However, since this project is not a Eclispe PDE project we are going to manage our class path dependencies in the typical Java project way.  To do that we have to add Spring_DM_1.1 library we created earlier to the class path of the project. 

 

Our project is very simple and contains only one class StringReverserTest.java. To save time we are going to skip the details of how to create Java project in Eclipse . 

Instead use Import Existing Project into the workspace feature provided by Eclipse and import org.springframework.osgi.service.test project form the resources provided

 

Once done your project should look like this:

Read the comments to get details on each test method.

The only thing worth pointing out in this tutorial is the two methods provided by Spring DM testing framework that help us bootstrap our test:

==================================================================================== 
public Resource[] getTestBundles() {
return new Resource[] { new FileSystemResource(
"../org.springframework.osgi.springdm.simple/plugins/
                 org.springframework.osgi.springdm.simple_1.0.0.jar") };
}
====================================================================================

This method will deploy the bundle we’ve created and packaged in the previous section inside of Spring DM test environment.

 

[Note]Note:
The relative path assumes that both projects are in the same Eclipse workspace.


====================================================================================
public String getRootPath() {
return"file:./bin";
}
====================================================================================

Our code is compiled (by default) into bin directory at the root of our project, however Spring-DM Test framework will be looking in the default directory for our classes and resources. This method will override default location (./target/test-classes) of the root path for the on-the-fly bundle created by Spring-DM Testing framework. 

 

 

Run the test as JUnit test and study the output.

Open up StringReverserTest.java, click on Run -> Run As -> JUnit Test

 

 

You should see that our bundle was deployed, service exported and tested

====================================================================================
. . . . . . .
906 [main] INFO
org.springframework.osgi.test.AbstractOptio. . .$EmptyOsgiApplicationContext -
Publishing application context as OSGi service with properties
{org.springframework.context.service.name=
TestBundle-testOsgiPlatformStarts-org.spr. . .k.osgi.service.test.StringReverserTest,
Bundle-SymbolicName=
TestBundle-testOsgiPlatformStarts-org.spr. . .k.osgi.service.test.StringReverserTest,
Bundle-Version=0.0.0}
Eclipse
1.3.0
OSGi/Minimum-1.0,OSGi/Minimum-1.1,. . .,. . .,J2SE-1.3,J2SE-1.4,J2SE-1.5,JavaSE-1.6
OSGi System Bundle,
AOP Appliance API,
JUnit,
ObjectWeb ASM,
SLF4J API,
SLF4J Log4J Binding,
SLF4J Jakarta Commons Logging Over SLF4J Binding,
spring-aop,
spring-beans,
spring-context,
spring-core,
spring-test,
log4j.osgi,
spring-osgi-annotation,
spring-osgi-core,
spring-osgi-extender,
spring-osgi-io,
spring-osgi-test,
Simple String Reverser Bundle,
TestBundle-tes. . .tarts-org.springframework.osgi.service.test.StringReverserTest,
938 [main] INFO org.springframework.osgi.sample.service.impl.StringReverserImpl -
Receiveing string: Hello Spring DM
938 [main] INFO org.springframework.osgi.sample.service.impl.StringReverserImpl -
Returning string: MD gnirpS olleH
938 [main] INFO org.springframework.osgi.service.test.StringReverserTest -
Reversed String: MD gnirpS olleH
2454 [Thread-0] INFO org.springframework.osgi.service.test.StringReverserTest -
Shutting down OSGi platform
. . . . . . .
====================================================================================
Continue. . .