Testing Controllers with Spring MVC Test and Spock

8,677 views

Published on

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.

Published in: Technology
0 Comments
11 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,677
On SlideShare
0
From Embeds
0
Number of Embeds
32
Actions
Shares
0
Downloads
132
Comments
0
Likes
11
Embeds 0
No embeds

No notes for slide

Testing Controllers with Spring MVC Test and Spock

  1. 1. Testing Controllers with Spring MVC Test and Spock by John Thompson Wednesday, September 11, 13
  2. 2. About Me Senior 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 miles Wednesday, September 11, 13
  3. 3. Session Overview Wednesday, September 11, 13
  4. 4. Introduction to Spring MVC Test Introduction to testing with Spock Using Spring MVC Test with Spock Wednesday, September 11, 13
  5. 5. Spring MVC Test Wednesday, September 11, 13
  6. 6. Why Spring MVC Test Testing 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
  7. 7. 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
  8. 8. 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
  9. 9. Test Setup Creating the MockMVC Controller Wednesday, September 11, 13
  10. 10. Simple Controller Test Very simple example of a controller unit test Wednesday, September 11, 13
  11. 11. Testing Query String Parameters Example of testing URL params Wednesday, September 11, 13
  12. 12. Testing Form Binding Wednesday, September 11, 13
  13. 13. Testing Media Type XML Wednesday, September 11, 13
  14. 14. Testing Media Type JSON Wednesday, September 11, 13
  15. 15. Spock Wednesday, September 11, 13
  16. 16. About Groovy Groovy 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
  17. 17. 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
  18. 18. Groovy Vs Java These two classes are roughly the same Wednesday, September 11, 13
  19. 19. Groovy Typing Optional 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
  20. 20. Closures Wednesday, September 11, 13
  21. 21. Groovy Maps & Lists In Action Wednesday, September 11, 13
  22. 22. 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
  23. 23. 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
  24. 24. Why Use BDD? BBD is: More Expressive of the intention of the test Easy to Understand by Developers and nonDevelopers a technique for help Developers focus on the objective of the test A tool to communicate to non-developers the Wednesday, September 11, 13
  25. 25. Example Spock Spec A non-technical person can read this example specification and understand the intention of the test. Wednesday, September 11, 13
  26. 26. 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
  27. 27. Spock Basics When, Then Wednesday, September 11, 13
  28. 28. Spock Basics Given, When, Then Wednesday, September 11, 13
  29. 29. Spock Basics Expect, Where Easy to use syntax to loop over expected Wednesday, September 11, 13
  30. 30. Spock Basics Enhanced Error Reporting Wednesday, September 11, 13
  31. 31. Spock Mocks Mocks in Spock are easy to create Mock Actions are also easily specified Wednesday, September 11, 13
  32. 32. Spring MVC Test and Spock Wednesday, September 11, 13
  33. 33. 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
  34. 34. 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
  35. 35. Spring MVC Test / Spock Example Wednesday, September 11, 13
  36. 36. Realistic Example of Spock Controller Some other Service Wednesday, September 11, 13 Service Spring Integration
  37. 37. Code Examples Spock with Mock Service Testing Parameter Parsing Testing JSON Response Testing Spring Context Wednesday, September 11, 13
  38. 38. Questions Wednesday, September 11, 13

×