Java EE Behave!!!!

27
base2Services Pty Ltd Commercial in Confidence 2010 #jeebehave Java EE Behave!!!! Behaviour-Driven Development with Java EE Aaron Walker [email protected] @aaronwalker http://aaronwalker.me http://www.base2services.com

description

Behavior-driven development (BDD) is an evolution of test-driven development (TDD). It shifts the vocabulary from being test-based to behavior-based and positions itself as a design philosophy. JBehave is a Java framework for BDD, and this session explores how to write integration tests for your Java EE apps with JBehave. It also demonstrates how to leverage the Context and Dependency Injection (CDI) APIs to implement your tests. This session is a must-see for all Java EE developers who want a better way to write integration tests aligned with the intended behavior.

Transcript of Java EE Behave!!!!

Page 1: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010#jeebehave

Java EE Behave!!!!Behaviour-Driven Development with Java EE

Aaron [email protected]

@aaronwalkerhttp://aaronwalker.me

http://www.base2services.com

Page 2: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Agenda

• What is this BDD thingy

• JBehave

• How to make your tests behave!

• { Demo }

• Java EE flavouring

• { Demo }

• Q & A

Page 3: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

BDD ~ Behaviour Driven Development ~

“BDD facilitates agile development, which is an approach to develop functional software, within

reasonable timeline, making everyone happy, without killing anyone, orburning down any bank.”

Page 4: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

BDD's Core Principles

• It's all behaviour“The business & technology people should be speaking the same words when referring to the same idea,there should not be any translator.”

Behaviour: the addition of N values should yield the summation of them

Example: the addition of 2 + 4 + 2 should yield 8

Page 5: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

BDD's Core Principles

• The evolution of test-driven development (TDD) and acceptance-test driven design

• Shifts the vocabulary from being test-based to behaviour-based

• Essentially a design philosophy

Page 6: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010#jeebehave

Unit testing frameworks mismatch

It’s all about the Behaviour

Page 7: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Oh JBehave• a framework for Behaviour-Driven

Development.

• pure Java implementation

• Users can specify and run plain text-based user stories

• Annotation-based configuration and Steps class specifications

• Dependency Injection support (more on this one later)

• Tool/IDE integration

• Ant, Maven, Selenium, Eclipse, IntelliJ, Netbeans

Page 8: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Yep....plain text

Page 9: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Narrative

• Describes the overall intent of the story

• Generally describes a single feature or use case

• Should include any participating actors

“In order to learn about BDD and JBehave as a JavaOne attendee I will attend session 24421- JEE Behave!”

Page 10: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Scenario

• Describes a specific interaction

• Generally fine-grained

• test valid and invalid outcomes

• Support for parametrisation

“Scenario: User signs up with invalid data”

Page 11: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Given|When|Then|And

• Given ... setting up system to a known state

• When ... exercising an event

• Then ... verifying an outcome

• And ... used to chain multiple Given, When and Then statements

Page 12: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

So where’s the Java?• Mapping Textual Scenario Steps to Java Methods via

annotations

• Steps instances may inherit or implement other class/interface as required by the model of the application under test.

• allows many different ways to configure Embeddable Java classes

• ConfigurableEmbedder: allows the specification of the Configuration and CandidateSteps.

• InjectableEmbedder: allows the injection of a fully specified Embedder.

Page 13: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

JUnit

• JUnit is supported out-of-the-box via several Embeddables implementations:

• JUnitStory: provides a one-to-one mapping with the textual story via the StoryPathResolver.

• JUnitStories: provides a many-to-one mapping with the textual story paths explicitly specified by overriding the storyPaths() method.

Page 14: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

One Step at a time

Page 15: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

And the config

• Fluent API for specifying configuration

Page 16: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

{ Demo }Enough Slide-ware

Show me the Code!!!

Page 17: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Now lets sprinkle a little bit of JEE on top

Page 18: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

CDI/Weld

• Contexts and Dependency Injection for Java

• (JSR-299 included in JEE6)

• Weld is the reference implementation

• http://seamframework.org/Weld

Page 19: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

JBehave CDI support

• jbehave-weld module

• Uses Weld Java-SE integration to bootstrap the CDI container

• can inject configuration and steps using CDI annotations

• greatly simplifies the configuration and management of step classes

Page 20: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Inject the Configuration

• Use a CDI @Produces along with a @WeldConfiguration qualifier annotation

• Gets automatically injected into the running scenario

Page 21: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

What about the Steps

• Simply mark Step classes with a @WeldStep annotation

• They get automatically added as candidate steps

• Steps can use @Inject to inject dependencies

Page 22: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

{ Demo }Show me a better way!!!

Page 23: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

JBehave Web

• JBehave Web is an extension of the JBehave framework providing a web integration layer

• Features include:

• Web Queue to allow generic stories to be run asynchronously via a simple web interface.

• Web Runner to allow generic stories to be run synchronously via a simple web interface.

• Selenium integration module to allow automation of stories for web applications using Selenium.

Page 24: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

{ Demo }Now let’s put all this together

Page 25: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

System Integration Testing

• JBehave is a good language for describing system integration

• BA’s, QA’s, architects, and developers are all speaking the same language

• Self documented acceptance tests

Page 26: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

What’s Next

• Custom CDI Scopes

• @StoryScope, @ScenarioScope

• Arquillian Integration

• ???????

Page 27: Java EE Behave!!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Q & A

• http://behaviour-driven.org/

• http://jbehave.org - JBehave project site

• github.com/aaronwalker/JavaOne2011 - demo code

• http://aaronwalker.me - blog

[email protected] - email

• @aaronwalker - twitter