Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel
-
Upload
mfrancis -
Category
Technology
-
view
832 -
download
3
description
Transcript of Testing OSGi the "groovy" way - Lars Pfannenschmidt, Dennis Nobel
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)