testing controllers with spring mvc test and spock

38

Click here to load reader

Upload: spring-io

Post on 13-May-2015

3.572 views

Category:

Technology


0 download

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.

TRANSCRIPT

Page 1: Testing Controllers with Spring MVC Test and Spock

Testing Controllers with Spring MVC Test

and Spock by John Thompson

Wednesday, September 11, 13

Page 2: Testing Controllers with Spring MVC Test and Spock

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

Page 3: Testing Controllers with Spring MVC Test and Spock

Session Overview

Wednesday, September 11, 13

Page 4: Testing Controllers with Spring MVC Test and Spock

Introduction to Spring MVC Test

Introduction to testing with Spock

Using Spring MVC Test with Spock

Wednesday, September 11, 13

Page 5: Testing Controllers with Spring MVC Test and Spock

Spring MVC Test

Wednesday, September 11, 13

Page 6: Testing Controllers with Spring MVC Test and Spock

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

Page 7: Testing Controllers with Spring MVC Test and Spock

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

Page 8: Testing Controllers with Spring MVC Test and Spock

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

Page 9: Testing Controllers with Spring MVC Test and Spock

Test Setup

Creating the MockMVC Controller

Wednesday, September 11, 13

Page 10: Testing Controllers with Spring MVC Test and Spock

Simple Controller Test

Very simple example of a controller unit test

Wednesday, September 11, 13

Page 11: Testing Controllers with Spring MVC Test and Spock

Testing Query String Parameters

Example of testing URL params

Wednesday, September 11, 13

Page 12: Testing Controllers with Spring MVC Test and Spock

Testing Form Binding

Wednesday, September 11, 13

Page 13: Testing Controllers with Spring MVC Test and Spock

Testing Media Type - XML

Wednesday, September 11, 13

Page 14: Testing Controllers with Spring MVC Test and Spock

Testing Media Type - JSON

Wednesday, September 11, 13

Page 15: Testing Controllers with Spring MVC Test and Spock

Spock

Wednesday, September 11, 13

Page 16: Testing Controllers with Spring MVC Test and Spock

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

Page 17: Testing Controllers with Spring MVC Test and Spock

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

Page 18: Testing Controllers with Spring MVC Test and Spock

Groovy Vs JavaThese two classes are roughly the same

Wednesday, September 11, 13

Page 19: Testing Controllers with Spring MVC Test and Spock

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

Page 20: Testing Controllers with Spring MVC Test and Spock

Closures

Wednesday, September 11, 13

Page 21: Testing Controllers with Spring MVC Test and Spock

Groovy Maps & Lists In Action

Wednesday, September 11, 13

Page 22: Testing Controllers with Spring MVC Test and Spock

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

Page 23: Testing Controllers with Spring MVC Test and Spock

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

Page 24: Testing Controllers with Spring MVC Test and Spock

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

Page 25: Testing Controllers with Spring MVC Test and Spock

Example Spock Spec

A non-technical person can read this example specification and understand the intention of the test.

Wednesday, September 11, 13

Page 26: Testing Controllers with Spring MVC Test and Spock

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

Page 27: Testing Controllers with Spring MVC Test and Spock

Spock Basics

When, Then

Wednesday, September 11, 13

Page 28: Testing Controllers with Spring MVC Test and Spock

Spock BasicsGiven, When, Then

Wednesday, September 11, 13

Page 29: Testing Controllers with Spring MVC Test and Spock

Spock Basics

Expect, Where

Easy to use syntax to loop over expected

Wednesday, September 11, 13

Page 30: Testing Controllers with Spring MVC Test and Spock

Spock Basics

Enhanced Error Reporting

Wednesday, September 11, 13

Page 31: Testing Controllers with Spring MVC Test and Spock

Spock MocksMocks in Spock are easy to create

Mock Actions are also easily specified

Wednesday, September 11, 13

Page 32: Testing Controllers with Spring MVC Test and Spock

Spring MVC Test and Spock

Wednesday, September 11, 13

Page 33: Testing Controllers with Spring MVC Test and Spock

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

Page 34: Testing Controllers with Spring MVC Test and Spock

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

Page 35: Testing Controllers with Spring MVC Test and Spock

Spring MVC Test / Spock Example

Wednesday, September 11, 13

Page 36: Testing Controllers with Spring MVC Test and Spock

Realistic Example of Spock

Controller Service

Spring Integration

Some other Service

Wednesday, September 11, 13

Page 37: Testing Controllers with Spring MVC Test and Spock

Code Examples

Spock with Mock Service

Testing Parameter Parsing

Testing JSON Response

Testing Spring Context

Wednesday, September 11, 13

Page 38: Testing Controllers with Spring MVC Test and Spock

Questions

Wednesday, September 11, 13