testing osgi the "groovy" way - lars pfannenschmidt, dennis nobel

32
EclipseCon Europe 2013 Lars Pfannenschmidt & Dennis Nobel

Upload: mfrancis

Post on 28-May-2015

832 views

Category:

Technology


3 download

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

Page 1: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

EclipseCon Europe 2013

TESTING�OSGI�THE�GROOVY�W?Y

Lars Pfannenschmidt & Dennis Nobel

Page 2: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

Lars PfannenschmidtSenior Software Engineer at Level Up Analytics IntuitFounder of

ÔÔ @mobilecgn

Page 3: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

Dennis NobelIT Consultant at itemis AG Java, Web and OSGi Developer

Ô @itemisÔ

Page 4: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

IM?GINEwriting tests would make fun...

Page 5: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

QUESTION�1:Do you test your OSGi

components?

Page 6: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

QUESTION�2:Are you using Groovy?

Page 7: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

Our Testing StoryEmbedded Device with CVM (~Java 1.4)OSGi framework (Prosyst mBS)Development with EclipseCI Build with Maven and Tycho

ÔÔÔÔ

Page 8: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

How to write our tests?

Page 9: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

Test RequirementsEasy to writeModern languageType-safe access to Java classesEasy mockingExecutable in Eclipse and Maven CI buildGood IDE supportNo side effects for production code

ÔÔÔÔÔÔÔ

Page 10: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

J?V?�Modern language?

Not really...

Page 11: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel
Page 12: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

What? Why Groovy?

Page 13: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

Because we can!

Page 14: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

GROOVYJVM LanguageSyntax SugarClosuresLess "Chatty"

ÔÔÔÔ

Page 15: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

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) }

    

Page 16: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

How to test our OSGi Services?

Page 17: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

Pizza Service SampleServices: PizzaService → PaymentService

            

public class PizzaServiceImpl implements PizzaService {

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

        

Page 18: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

UNIT�TESTS+ Executed fast+ Focussed on the component­ No OSGi Runtime features­ Dependencies must be mocked­ No testing of declarative OSGi parts

Page 19: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

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

Page 20: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

IN�CONT?INER�TESTSExecuted in OSGi environmentFocussed on the componentTesting of declarative OSGi partsOnly partial mocking

ÔÔÔÔ

Page 21: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

...we can not run Groovy in CVM

Page 22: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

Why not use

EQUINOXwith a modern JVM?

Page 23: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

GROOVYIn-Container Testing with

equinox

Page 24: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

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

        

Page 25: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

Register OSGi service mocks (1)            

class PizzaServiceTest extends OSGiTest {

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

        

Page 26: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

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)}

        

Page 27: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

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))} ...

        

Page 28: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

?UTOM?TIONwith Maven and Tycho

Page 29: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

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>

        

Page 30: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

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>...

        

Page 31: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

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!

ÔÔÔÔ

Ô

Page 32: Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel

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)