behavior-driven development and lambdaj

27
BDD + Lambdaj Andreas Enbohm, Capgemini @enbohm

Upload: andreas-enbohm

Post on 25-May-2015

449 views

Category:

Technology


2 download

DESCRIPTION

A short presentation of BDD (Behavior-driven Development) and LambdaJ (https://code.google.com/p/lambdaj/) used in one of my client projects.

TRANSCRIPT

Page 1: Behavior-driven Development and Lambdaj

BDD + Lambdaj

Andreas Enbohm, Capgemini@enbohm

Page 2: Behavior-driven Development and Lambdaj

BDD

• Behavior-driven development (BDD) is an increasingly popular variation on test-driven development, which helps developers think more in terms of “executable specifications” than in terms of conventional tests

Page 3: Behavior-driven Development and Lambdaj

Why use BDD?

• Only build feature that add real value• Less wasted effort• Better communication• Higher quality, better tested product• Traceability

Page 4: Behavior-driven Development and Lambdaj

BDD

• BDD describes TDD done well- removes word ’test’- replace it with behaviour, examples, scenarios etc.

• Good habits;1. Working outside-in2. Using examples to clarify requirements (scenarios)3. Using a common (ubiquitous) language

Page 5: Behavior-driven Development and Lambdaj

BDD – Value Driven

• A good goal should add value to business- increase revenue- reduce cost- avoid future cost- protect revenue

”Increase revenue by allowing customers to…”

”Prevent current customers switching to competing product by …”

Page 6: Behavior-driven Development and Lambdaj

BDD – What Does the Customer Need?• Good teams push back!

- users tend to express requirements as implementations- we need to find the business need!

We need caching Why?

So that the page loads fast

So that the customer will buy

stuff on our site and doesn’t leave page

Why?

Ok, so to increase the chance a customer will buy [stuff] it need to be ’fast’. Caching

might be one way to achive this, but compressing images might be more

effective. Or another way might be loaders, or ..

Page 7: Behavior-driven Development and Lambdaj

BDD – Better understanding• Working outside-in

- if you need to explain to a computer how to check the requirement, you’ll need to be damn sure understand it yourself

• Business black-box testing is often done manually!- BDD enables automated acceptance testing

• Mininum effort /code to fulfull requirement

Page 8: Behavior-driven Development and Lambdaj

BDD – Common Language• Common Language

- communication often the biggest overhead- even bigger if you allow different dialects of terminology- takes time but gains significant advantage

Page 9: Behavior-driven Development and Lambdaj

BDD

Page 10: Behavior-driven Development and Lambdaj

BDD

Page 11: Behavior-driven Development and Lambdaj

BDD - Stories

Story: Basic Purchase flow - User purchases a Grundpaket (id 21)Narrative:In order to attract new CDK customersAs a leading channel resellerI want to provide an easy way to buy our products through our website

Goal come first

Stakeholder is secondary

The feature must be required to achieve this

goal

Page 12: Behavior-driven Development and Lambdaj

BDD - Scenarios

• The ’perfect’ bridge between the business-facing and technology-facing sides of a team

Scenario: User selects a channel package for purchase (id 21.2)

Given address search is done with address street Hantverkaregatan streetNo 28 and city Helsingborg on page url http://...

When user select totalpaketet by clicking on choose button

Then the user starts a purchase flow where the first step is called Grundpaket och box is shown

Common language which is reflected all way down in the

code

Page 13: Behavior-driven Development and Lambdaj

BDD - Java

• CDK use Jbehave- lots of documentation- uses Gherkin language

Page 14: Behavior-driven Development and Lambdaj

BDD - Implementation

Page 15: Behavior-driven Development and Lambdaj

BDD – CDK Implementation

• No stories/scenarion from start- Emma wrote a lot of BDD stories- used by CDK as ’lightweight’ acceptance test

• Selenium- browser automation

• Spring as embedded runner- gives us DI

• Page template patterns (see code)

Page 16: Behavior-driven Development and Lambdaj

BDD – CDK Implementation

• Pros- another way of thinking of test- sentence instead of test (even for our Junit tests)- we could make our major refactoring with confidence

• Cons- selenium (engines)- issus/bugs with JBehave (fetching parameters, mapping to POJO)- we should not come up with the goal

Page 17: Behavior-driven Development and Lambdaj

BDD

• Reports show 75%* less bugs and 30%* faster to production

Page 18: Behavior-driven Development and Lambdaj

Lambdaj

• An internal DSL to manipulate collections without loops

• Large part of business logic is always the same; iteration over collections of business objects to do a set of tasks

• Loops (especially nested and mixed with conditions) are hard to read that to be written

• for loop is inherently serial

Page 19: Behavior-driven Development and Lambdaj

Lambdaj

• Business logic should be less technical and closer to business fashion

Page 20: Behavior-driven Development and Lambdaj

Lambdaj

• Iterate over collections in order to:- select- aggregate- sort- filter- group- extract- and more..

Page 21: Behavior-driven Development and Lambdaj

Lambdaj - Sort

List<Person> sortedByAgePersons = sort(persons, on(Person.class).getAge());

List<Person> sortedByAgePersons = new ArrayList<Person>(persons);Collections.sort(sortedByAgePersons, new Comparator<Person>() {        public int compare(Person p1, Person p2) {           return Integer.valueOf(p1.getAge()).compareTo(p2.getAge());        }});

Old Java style

Lambdaj

Page 22: Behavior-driven Development and Lambdaj

Lambdaj - Select

List<Sale> sales = select(sales, having(on(Sale.class).getBuyer(), equalTo(selectMin(persons, on(Person.class).getAge()))));

Person youngest = null;for (Person person : persons) if (youngest == null || person.getAge() < youngest.getAge()) youngest = person; List<Sale> buys = new ArrayList<Sale>(); for (Sale sale : sales) if (sale.getBuyer().equals(youngest)) buys.add(sale);

Old Java style

Lambdaj

Page 23: Behavior-driven Development and Lambdaj

Lambdaj - Sum

Integer totalSumForStandaloneChannels = sum(standaloneChannels,on(TvChannel.class).getPrice().getMonthlyFee());

Integer totalSumForStandaloneChannels = 0;for (TvChannel channel: standaloneChannels) {

totalSumForStandaloneChannels += channel.getPrice.getMonthlyFee()}

Old Java style

Lambdaj

Page 24: Behavior-driven Development and Lambdaj

Lambdaj - Group

Group<ChannelPackage> mutuallyExclusiveGroup = group(selectedAddonPackages,

by(on(AddonChannelPackage.class).getGroup().getType()), by(on(AddonChannelPackage.class).getGroup().getName()));

Don’t bother….

Old Java style

Lambdaj

Page 25: Behavior-driven Development and Lambdaj

Lambdaj - Index

Map<String, Car> carsByBrand = index(cars, on(Car.class).getBrand());

Map<String, Car> carsByBrand = new HashMap<String, Car>();for (Car car : db.getCars()) carsByBrand.put(car.getBrand(), car);

Old Java style

Lambdaj

Page 26: Behavior-driven Development and Lambdaj

Lambdaj – Bulk Operations

List<Person> personInFamily = asList(new Person(“Chuck"), new Person(“Lone Wolf"));

forEach(personInFamily).setLastName(“Norris");

Page 27: Behavior-driven Development and Lambdaj

Lambdaj - Closures

• Lambdaj Closures

Closure println = closure(); { of(System.out).println(var(String.class));}

//invoked by "closing" its free variable once:println.apply("one");

//or more times:println.each("one", "two", "three");