Click here to load reader
Upload
spring-io
View
3.581
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Speaker: John Thompson Everyone knows controllers can be tricky little buggers to test. Spring MVC Test brought some exciting testing capabilities to Spring 3.2, but it you're left on your own for mocking the service layer in your controllers. We'll take a look at the Groovy approach of using Spock to unit test your controller interactions with the service layer and Spring MVC Test to unit test controller interactions with the web layer.
Citation preview
Testing Controllers with Spring MVC Test
and Spock by John Thompson
Wednesday, September 11, 13
About MeSenior Software Engineer with Incept5
20 years of software development experience
Crazy Triathlete Currently Training for:
Miami Half-Ironman 70.3
1.2 mile swim; 56 mile bike; 13.1 mile run
72 Hrs to Key West
3 days of cycling, 280 milesWednesday, September 11, 13
Session Overview
Wednesday, September 11, 13
Introduction to Spring MVC Test
Introduction to testing with Spock
Using Spring MVC Test with Spock
Wednesday, September 11, 13
Spring MVC Test
Wednesday, September 11, 13
Why Spring MVC TestTesting Controllers outside the web context is diffcult
How do you test the behavior of all that Spring ‘magic’
Annotations?
Form Binding?
Parameters?
Parsing JSON/XML?Wednesday, September 11, 13
Enter Spring MVC Test
Spring MVC Test enables unit testing of controllers by:
Using MockHttpServletRequest/Response
Controllers are invoked as they would be through the Spring MVC Dispatcher Servlet
Thus all the annotation & binding magic can be tested
Wednesday, September 11, 13
Spring MVC Test History
Originally Started as a stand alone project for Spring 3.1.
Moved into Spring Framework in version 3.2 RC1
Wednesday, September 11, 13
Test Setup
Creating the MockMVC Controller
Wednesday, September 11, 13
Simple Controller Test
Very simple example of a controller unit test
Wednesday, September 11, 13
Testing Query String Parameters
Example of testing URL params
Wednesday, September 11, 13
Testing Form Binding
Wednesday, September 11, 13
Testing Media Type - XML
Wednesday, September 11, 13
Testing Media Type - JSON
Wednesday, September 11, 13
Spock
Wednesday, September 11, 13
About GroovyGroovy is a JVM based language specifically designed to work in conjunction with Java.
Groovy is easy to learn. You can write Groovy in 100% Java Syntax.
Groovy is Dynamic. Meaning methods and Properties can be added at runtime.
Groovy is Concise and Expressive
Say Adios to a lot of ceremonial code
Wednesday, September 11, 13
Syntax Differences
The semicolon is optional for line termination
you are not required to catch declared Exceptions
Getters and Setters are automatically provided
Can also be accessed via the ‘DOT’ notation
() and [] are optional
Wednesday, September 11, 13
Groovy Vs JavaThese two classes are roughly the same
Wednesday, September 11, 13
Groovy TypingOptional Typing - User ‘def’ to declare properties
Once a type is assigned, the type CANNOT change
Duck Typing - if it looks like a duck, walks like a duck, quacks like a duck, then it is a duck.
This means you can use any type that implements the desired method or property
Very powerful paradigm
Wednesday, September 11, 13
Closures
Wednesday, September 11, 13
Groovy Maps & Lists In Action
Wednesday, September 11, 13
What Is Spock?
Spock is a testing a specification Framework for Java and Groovy Applications.
Supports traditional Unit, Integration, and Functional Testing
Supports Test Driven Development (TDD)
Designed to Support Behavior Driven Development(BDD)
Wednesday, September 11, 13
Behavior Driven Development
Paradigm Shift from Test Driven Development
Focus is to test Behaviors
Given - “I Have a Car and Car Keys”
When - “I insert the Key and turn”
THEN - “The Engine of the Car Starts”
Leads to descriptive testing ‘Specifications’ of
Wednesday, September 11, 13
Why Use BDD?BBD is:
More Expressive of the intention of the test
Easy to Understand by Developers and non-Developers
a technique for help Developers focus on the objective of the test
A tool to communicate to non-developers the
Wednesday, September 11, 13
Example Spock Spec
A non-technical person can read this example specification and understand the intention of the test.
Wednesday, September 11, 13
Why Spock?Spock is designed to support BDD and TDD.
Spock uses Groovy and has a clean, concise and expressive language.
Spock has extensive mocking support. (Vs JUnit which has none)
Spock is extendable
Spring
Geb (Functional Web Testing
Wednesday, September 11, 13
Spock Basics
When, Then
Wednesday, September 11, 13
Spock BasicsGiven, When, Then
Wednesday, September 11, 13
Spock Basics
Expect, Where
Easy to use syntax to loop over expected
Wednesday, September 11, 13
Spock Basics
Enhanced Error Reporting
Wednesday, September 11, 13
Spock MocksMocks in Spock are easy to create
Mock Actions are also easily specified
Wednesday, September 11, 13
Spring MVC Test and Spock
Wednesday, September 11, 13
Designing Controllers for Testability
Controller should focus on one thing
Handling the web request.
Should not be invoking services, or interacting with the database.
Should not worry about transactions.
Should minimal (if any) business logic
Wednesday, September 11, 13
Designing Controllers for Testability (Cont.)
All business logic should reside in a service layer.
This allows us to inject mocks into the the controllers in the support of unit testing.
Wednesday, September 11, 13
Spring MVC Test / Spock Example
Wednesday, September 11, 13
Realistic Example of Spock
Controller Service
Spring Integration
Some other Service
Wednesday, September 11, 13
Code Examples
Spock with Mock Service
Testing Parameter Parsing
Testing JSON Response
Testing Spring Context
Wednesday, September 11, 13
Questions
Wednesday, September 11, 13