Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Eclipsecon 2019 testing OSGi applications

Testing OSGi applications is a little different from other platforms. This talk aims to get people started with testing on OSGi and provide an overview of the options as well as practical examples.

  • Be the first to comment

Eclipsecon 2019 testing OSGi applications

  1. 1. Testing OSGi Applications Christian Schneider Adobe
  2. 2. Christian Schneider š Computer scientist at Adobe š Apache member and committer š Twitter @schneider_chris š Website
  3. 3. Properties of valuable Tests Kent Beck šIsolated šComposable šFast šInspiring šWritable šReadable šBehavioral šStructure-insensitive šAutomated šSpecific šDeterministic šPredictive
  4. 4. Test pyramid UI Acceptance Integration Unit
  5. 5. Unit tests for OSGi š Use standard junit ( No OSGi framework) š Avoid OSGi APIs - difficult to test š Try to be as black box as possible
  6. 6. Unit testing DS components @Component public class TaskResource { @Reference private TaskService service; @Context private UriInfo uriInfo; … } How to populate private fields (from DS, JAX-RS, …) ?
  7. 7. Using Mockito for DS tests @RunWith(MockitoJUnitRunner.class) public class TaskResourceTest { @Mock TaskService taskService; @Mock UriInfo uriInfo; @InjectMocks TaskResource taskRes; … }
  8. 8. Integration tests š Test how code integrates with environment š Start OSGi environment š Black box via OSGi services š Minimal environment to make tests fast
  9. 9. Integration testing (Testing frameworks) Depends on how you package your application Bndtools bnd testing Karaf pax exam Karaf container Small tests pax exam OSGi container
  10. 10. Bnd testing š Define application via bndrun š Very near to real application š Test is fast ~0.5s š In process -> Easy debug š No easy way to inject OSGi services. Have to use ServiceTracker. š Needs special runner (can not use surefire) š Test in src/main/java
  11. 11. Pax exam š @Configuration inside test. š Karaf container or plain OSGi container š In process or forked š Injection of service š Works with surefire š Test runs inside OSGi @RunWith(PaxExam.class) public void MyTest { @Configuration public Option configuration() { return composite( … ); } @Inject TaskService taskService; }
  12. 12. Pax exam plain OSGi š Must manage all bundles to start š Best if code only needs a few bundles š Code and Test can be inside same module š In process -> Easy debugging š Fast ~0.5s @Configuration public Option configuration() { return composite( mavenBundle(“groupId”, “artifactId”) .versionAsInProject(), bundle("reference:file:target/classes/") … ); }
  13. 13. Pax exam Apache Karaf container š Starts real Karaf š Forked -> only remote debug š Karaf features allow complex applications š Much slower compared to OSGi container or bnd testing ~10 seconds karafDistributionConfiguration() .frameworkUrl(karafUrl) .useDeployFolder(false), keepRuntimeFolder(), features(ariesJaxRsRepo, "aries-jax-rs-whiteboard", "aries-jax-rs-whiteboard-jackson"),
  14. 14. System tests š Black box on external interfaces (REST, messaging, UI) š Use standard tools š Start full application š Use docker for backends Test Application Database In Process Forked process Docker
  15. 15. JAX-RS Tests with JAX-RS-whiteboard @Inject ClientBuilder clientBuilder; @Before public void before() { target ="http://localhost:8181/tasks"); } @Test public void testGetAll() { // Easy .. But has timing issue Response response = target.request().get(); }
  16. 16. Resilience against timing issues Awaitility When is your system ready to be called? š Awaitility can retry actions š Needs to be installed into OSGi runtime Response response = await() .atMost(10, SECONDS) .ignoreExceptions() .until(()->target.request().get(), notNullValue());
  17. 17. Running infra in docker safely Testcontainers @ClassRule public static GenericContainer redis = new GenericContainer("redis:3.0.2").withExposedPorts(6379); • Manages docker containers • Nice API for interaction with well known infra • Safely stops containers even if test crashes
  18. 18. Questions ? Examples and Resources: Follow me at Twitter @schneider_chris My blog