testing osgi the "groovy" way - lars pfannenschmidt, dennis nobel
DESCRIPTION
OSGi Community Event 2013 (http://www.osgi.org/CommunityEvent2013/Schedule) ABSTRACT In order to meet software project requirements, it is important to implement ongoing quality assurance using automated tests. However, for OSGi platforms which are used in various areas such as Embedded or Enterprise this turns out to be difficult. If several components need to be tested together, unit tests written in Java tend to reach their limits. Thus, implementing these tests can be very time consuming. Using the Groovy language, OSGi integration tests are efficient and easy to write, e.g. registering Groovy mocks as a OSGi Service turns out to be very handy. Hereby declarative OSGi components can be tested, too. Even if an OSGi application has to be CDC-compliant, tests can be written using Groovy in a modern syntax. This presentation demonstrates how to implement Groovy tests for a sample OSGi project and how Groovy tests can be executed in an Equinox OSGi environment. Furthermore, it shows how a continuous integration solution using Maven Tycho can look like. SPEAKER BIOS Lars Pfannenschmidt Interested in Mobile Applications, Smart Home, Domain Specific Languages, Machine Learning and agile development methodologies such as Scrum and Kanban. Senior Software Engineer and Founder of mobile.cologne Dennis Nobel Dennis works as an IT Consultant for itemis in the field of Java, Web and Mobile Development. Moreover he is interested in Agile Development, Continuous Integration, Modeling Technologies, Testing, IoT, Smart Home, OSGi and Groovy.TRANSCRIPT
EclipseCon Europe 2013
TESTING�OSGI�THE�GROOVY�W?Y
Lars Pfannenschmidt & Dennis Nobel
Lars PfannenschmidtSenior Software Engineer at Level Up Analytics IntuitFounder of
ÔÔ @mobilecgn
Dennis NobelIT Consultant at itemis AG Java, Web and OSGi Developer
Ô @itemisÔ
IM?GINEwriting tests would make fun...
QUESTION�1:Do you test your OSGi
components?
QUESTION�2:Are you using Groovy?
Our Testing StoryEmbedded Device with CVM (~Java 1.4)OSGi framework (Prosyst mBS)Development with EclipseCI Build with Maven and Tycho
ÔÔÔÔ
How to write our tests?
Test RequirementsEasy to writeModern languageType-safe access to Java classesEasy mockingExecutable in Eclipse and Maven CI buildGood IDE supportNo side effects for production code
ÔÔÔÔÔÔÔ
J?V?�Modern language?
Not really...
What? Why Groovy?
Because we can!
GROOVYJVM LanguageSyntax SugarClosuresLess "Chatty"
ÔÔÔÔ
Groovy JUnit TestType inference, readable asserts, syntax sugar and more:
@Testvoid '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 SampleServices: 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�CONT?INER�TESTSExecuted in OSGi environmentFocussed on the componentTesting of declarative OSGi partsOnly partial mocking
ÔÔÔÔ
...we can not run Groovy in CVM
Why not use
EQUINOXwith a modern JVM?
GROOVYIn-Container Testing with
equinox
MockingSimply 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)
@Testvoid '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))} ...
?UTOM?TIONwith Maven and Tycho
Tycho Groovy configuration (1)Groovy & Maven
http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven
Groovy Repository
<repositories> ... <repository> <id>groovy-eclipse</id> <layout>p2</layout> <url>http://dist.springsource.org/release/GRECLIPSE/e4.3/</url> </repository></repositories>
Tycho Groovy configuration (2)Tycho, Groovy & Surefire
...<plugin> <groupid>org.eclipse.tycho</groupid> <artifactid>tycho-surefire-plugin</artifactid> <version>${tycho.version}</version> <configuration> <includes> <include>**/*</include> </includes> ... </configuration></plugin>...
CONCLUSIONAdd integrative Tests to your OSGi projectsEasy to add groovy support in equinox and tychoGroovy makes developing tests more easierPowerful tests which run as fast as unit tests (>300 Tests in ~3minutes)Fun!
ÔÔÔÔ
Ô
1/32
TH?NK�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)