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.
1. Java EE Behave!!!!
Behaviour-Driven Development with Java EE
Aaron Walker
a.walker@base2services.com
@aaronwalker
http://aaronwalker.me
http://www.base2services.com
#jeebehave
base2Services Pty Ltd Commercial in Confidence 2010
2. Agenda
• What is this BDD thingy
• JBehave
• How to make your tests behave!
• { Demo }
• Java EE flavouring
• { Demo }
• Q &A
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
3. 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, or
burning down any bank.”
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
4. 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
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
5. 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
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
6. Unit testing
frameworks mismatch
It’s all about the Behaviour
#jeebehave
base2Services Pty Ltd Commercial in Confidence 2010
7. 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
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
8. Yep....plain text
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
9. 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!”
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
10. Scenario
• Describes a specific interaction
• Generally fine-grained
• test valid and invalid outcomes
• Support for parametrisation
“Scenario: User signs up with invalid data”
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
11. 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
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
12. 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.
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
13. 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.
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
14. One Step at a time
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
15. And the config
• Fluent API for specifying configuration
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
16. { Demo }
Enough Slide-ware
Show me the Code!!!
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
17. Now lets sprinkle a
little bit of JEE on top
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
18. CDI/Weld
• Contexts and Dependency Injection for
Java
• (JSR-299 included in JEE6)
• Weld is the reference implementation
• http://seamframework.org/Weld
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
19. 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
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
20. Inject the Configuration
• Use a CDI @Produces along with a
@WeldConfiguration qualifier annotation
• Gets automatically injected into the running
scenario
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
21. 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
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
22. { Demo }
Show me a better way!!!
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
23. 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.
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
24. { Demo }
Now let’s put all this together
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010
25. 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
#jeebehave
base2Services Pty Ltd 2011
base2Services Pty Ltd Commercial in Confidence 2010