EclipseCon Europe 2013
Lars Pfannenschmidt & Dennis Nobel
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)
}
Services: PizzaService → PaymentService
public class PizzaServiceImpl implements PizzaService {
@Override
public void placeOrder(final Order order) {
...
creditCardPaymentService.handleTransaction(...);
...
}
}
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
class PizzaServiceTest extends OSGiTest {
@Override
protected BundleContext getBundleContext() {
Activator.context
}
...
}
@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)
}
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))
}
...
...
groovy-eclipse
p2
http://dist.springsource.org/release/GRECLIPSE/e4.3/
...
org.eclipse.tycho
tycho-surefire-plugin
${tycho.version}
**/*
...
...
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)