Java EE Behave!!!!

Post on 18-Dec-2014

3.164 views 5 download

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

base2Services Pty Ltd Commercial in Confidence 2010#jeebehave

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

Aaron Walkera.walker@base2services.com

@aaronwalkerhttp://aaronwalker.me

http://www.base2services.com

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

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

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

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

base2Services Pty Ltd Commercial in Confidence 2010#jeebehave

Unit testing frameworks mismatch

It’s all about the Behaviour

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

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Yep....plain text

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!”

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”

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

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.

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.

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

One Step at a time

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

And the config

• Fluent API for specifying configuration

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

{ Demo }Enough Slide-ware

Show me the Code!!!

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

Now lets sprinkle a little bit of JEE on top

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

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

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

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

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

{ Demo }Show me a better way!!!

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.

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

{ Demo }Now let’s put all this together

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

base2Services Pty Ltd Commercial in Confidence 2010

base2Services Pty Ltd 2011

#jeebehave

What’s Next

• Custom CDI Scopes

• @StoryScope, @ScenarioScope

• Arquillian Integration

• ???????

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

• a.walker@base2services.com - email

• @aaronwalker - twitter