EclipseCon Europe 2013

Testing OSGi the
"groovy" way

Lars Pfannenschmidt & Dennis Nobel

Lars Pfannenschmidt

  • Senior Software Engineer at Level Up Analytics Intuit
  • Founder of @mobilecgn

Dennis Nobel

  • IT Consultant at itemis AG @itemis
  • Java, Web and OSGi Developer

Imagine

writing tests would make fun...

Question 1:

Do you test your OSGi components?

Question 2:

Are you using Groovy?

Our Testing Story

  • Embedded Device with CVM (~Java 1.4)
  • OSGi framework (Prosyst mBS)
  • Development with Eclipse
  • CI Build with Maven and Tycho

How to write our tests?

Test Requirements

  • Easy to write
  • Modern language
  • Type-safe access to Java classes
  • Easy mocking
  • Executable in Eclipse and Maven CI build
  • Good IDE support
  • No side effects for production code

Java?

Modern language?

Not really...

What? Why Groovy?

success-kid

Because we can!

Groovy

  • JVM Language
  • Syntax Sugar
  • Closures
  • Less "Chatty"

Groovy JUnit Test

Type inference, readable asserts, syntax sugar and more:

	   
@Test
void 'Build pizza with BBQ sauce only'() {
    
    def pizza = PizzaBuilder.newPizza()
                .withSauce(BBQ)
                .build()

    assertThat pizza.sauce, is(BBQ)
    
}
	   
    

How to test our OSGi Services?

Pizza Service Sample

Services: PizzaService → PaymentService

            
public class PizzaServiceImpl implements PizzaService {

    @Override
    public void placeOrder(final Order order) {
        ...
        creditCardPaymentService.handleTransaction(...);
        ...
    }
    
}
            
        

Unit Tests

  • + Executed fast
  • + Focussed on the component
  • - No OSGi Runtime features
  • - Dependencies must be mocked
  • - No testing of declarative OSGi parts

System Tests

  • + Cover complete system
  • + Real target environment
  • + No mocking or special configuration
  • - Embedded device is needed (high execution effort)
  • - Slow execution
  • - Not focussed

In-Container Tests

  • Executed in OSGi environment
  • Focussed on the component
  • Testing of declarative OSGi parts
  • Only partial mocking

...we can not run Groovy in CVM

Why not use

equinox

with a modern JVM?

Groovy

In-Container Testing with equinox

Mocking

Simply transform a map to a proxy:

            
def paymentService = [
    handleTransaction: { String companyId, long creditCardNumber ->
        assertThat companyId, is(equalTo('LUIGIS_PIZZA_SERVICE'))
        transactionCalled = true
    }
] as CreditCardPaymentService
            
        

Register OSGi service mocks (1)

            
class PizzaServiceTest extends OSGiTest {

    @Override
    protected BundleContext getBundleContext() {
        Activator.context
    }
    ...
}
            
        

Register OSGi service mocks (2)

            
@Test
void 'Assert that the handleTransaction method is called'() {
    ...
    registerMock(paymentService)

    PizzaService pizzaService = getService(PizzaService)

    def pizza = PizzaBuilder.newPizza().withSauce(Sauce.BBQ).build()
    def customerInfo = new CustomerInfo("Max Mustermann", new Address(), 4242)
    pizzaService.placeOrder(new Order(pizza, customerInfo))

    assertThat transactionCalled, is(true)
}
            
        

OSGiTest.groovy

            
def registeredServices = [:]
protected abstract BundleContext getBundleContext()

def <T> T getService(Class<T> clazz){
    def serviceReference = bundleContext.getServiceReference(clazz.name)
    assertThat serviceReference, is(notNullValue())
    bundleContext.getService(serviceReference)
}

def registerMock(def mock, Hashtable properties = [:]) {
    def interfaceName = mock.class.interfaces?.find({it})?.name
    assertThat interfaceName, is(notNullValue())
    registeredServices.put(interfaceName,
    bundleContext.registerService(interfaceName, mock, properties))
}
    ...
            
        

Automation

with Maven and Tycho

Tycho Groovy configuration (1)

Groovy & Maven
http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven

Groovy Repository

        

    ...
    
        groovy-eclipse
        p2
        http://dist.springsource.org/release/GRECLIPSE/e4.3/
    

            
        

Tycho Groovy configuration (2)

Tycho, Groovy & Surefire

            
...

    org.eclipse.tycho
    tycho-surefire-plugin
    ${tycho.version}
    
        
            **/*
        
        ...
    

...
            
        

Conclusion

  • Add integrative Tests to your OSGi projects
  • Easy to add groovy support in equinox and tycho
  • Groovy makes developing tests more easier
  • Powerful tests which run as fast as unit tests (>300 Tests in ~3 minutes)
  • Fun!

Thank You!

Checkout "groovy" OSGi testing at github:

        
$ git clone https://github.com/groovyosgi/testing.git
$ cd testing
$ mvn clean install
        
    

Lars Pfannenschmidt (Intuit, Inc.)

Dennis Nobel (itemis AG)