testing grails

29
© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission. Testing Grails Kenneth Kousen @kenkousen [email protected]

Upload: spring-io

Post on 13-Jul-2015

643 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Testing Grails

© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.

Testing Grails Kenneth Kousen

@kenkousen [email protected]

Page 2: Testing Grails

Grails

•  Full stack framework •  Convention over configuration •  DSLs written in Groovy

•  Home page: http://grails.org •  User guide: http://grails.org/doc/latest/

2

Page 3: Testing Grails

Philosophy

•  Never trust a test you haven’t seen fail

3

Page 4: Testing Grails

Testing

•  User guide •  http://grails.org/doc/latest/guide/testing.html •  Chapter 14 is latest version (2.4.3)

4

Page 5: Testing Grails

Generated tests

•  Any create-* or generate-* command •  Generates unit tests automatically

5

Page 6: Testing Grails

Unit vs Integration

•  Standard project structure •  test/unit •  test/integration

6

Page 7: Testing Grails

Unit vs Integration

•  Unit == No Grails infrastructure •  Database == concurrent hash map in memory

•  Integration uses Grails capabilities •  Transactions •  Dependency injection

7

Page 8: Testing Grails

Running tests

•  Command line: •  grails test-app

•  Optional arguments for: •  Specific package •  Specific class •  Specific artifacts (like all controllers)

8

Page 9: Testing Grails

Running tests

•  Interactive console •  Caching makes tests run much faster

grails>  test-­‐app  –unit  grails>  open  test-­‐report  

9

Page 10: Testing Grails

Spock

•  Grails uses Spock testing framework by default •  Grails 2.3+

10

Page 11: Testing Grails

Spock

•  Easy to read and learn •  Uses block structure •  Has powerful data driven tests

11

Page 12: Testing Grails

Spock

•  Write test method name as sentence •  Add expect: or when:/then: blocks •  Every statement in expect: or then: checked automatically

•  Uses Groovy truth

12

Page 13: Testing Grails

@TestFor

•  @TestFor annotation triggers “mixins” •  For controllers and services, generates fields

13

Page 14: Testing Grails

@Mock

•  Generates mock GORM method for domain classes

14

Page 15: Testing Grails

Testing Controllers

•  @TestFor instantiates controller

15

Page 16: Testing Grails

Testing Controllers

•  Testing render methods •  Invoke method and use response object •  Instance of GrailsMockHttpServletResponse  •  Has a getText() method

16

Page 17: Testing Grails

Testing Controllers

•  Testing redirects •  Invoke method and use response object •  Has a getRedirectedUrl() method

17

Page 18: Testing Grails

Testing Controllers

•  What if you need input parameters? •  Use params map •  Call set common properties

•  max, sort, offset, …

•  Can modify request, too request.method  =  ‘POST’  

18

Page 19: Testing Grails

Testing Controllers

•  Testing “return” methods •  Grails adds map to request and forwards to GSP

•  No need to test the forwarding (Grails infrastructure)

•  Assign method call to a map and check keys/values

19

Page 20: Testing Grails

Testing Rendering

•  If you render XML or JSON, •  Use xml or json property of response object •  Built with XmlSlurper or JsonSlurper  

•  Note: similar technique with request •  Assign to request.xml or request.json

20

Page 21: Testing Grails

Testing Rendering

•  Constants in ControllerUnitTestMixin  •  JSON_CONTENT_TYPE  •  TEXT_XML_CONTENT_TYPE  •  Many others

21

Page 22: Testing Grails

Testing Domain Objects

•  Tests use ConcurrentHashMap for DB •  Can test dynamic finders, criteria queries, …

22

Page 23: Testing Grails

Testing Constraints

•  Test constraints by creating a valid object and breaking it •  Use mockForConstraintsTests method

23

Page 24: Testing Grails

Spock

•  Spock includes where: block •  Anything that implements Iterable can be used

24

Page 25: Testing Grails

Expandos

•  Expando class has no methods or attributes •  Properties and methods can be added at runtime

 Expando  e  =  new  Expando()    e.myprop  =  3    e.doStuff  =  {  arg  -­‐>  /*  use  arg  somehow  */  }  

25

Page 26: Testing Grails

Mocks and Stubs

•  Method mockFor  •  First arg is class being mocked •  Second (optional) arg is “strict”

def  geoMock  =  mockFor(GeocoderService,  true)  geoMock.demand.fillInLatLng  {  Castle  c  -­‐>  }  controller.geocoderService  =  geoMock.createMock()  

26

Page 27: Testing Grails

Integration Tests

•  Uses Grails infrastructure •  Real DB (from test environment) •  Dependency injection •  Transactions

27

Page 28: Testing Grails

Functional tests

•  Nothing built into Grails •  Done through plugins

28

Page 29: Testing Grails

Plugins

•  Build test data •  http://grails.org/plugin/build-test-data •  Inspects constraints and generates valid instances

•  Functional •  Web driver functional: http://grails.org/plugin/webdriver •  Canoo WebTest: http://grails.org/plugin/webtest •  Selenium: http://grails.org/plugin/selenium •  Geb: http://grails.org/plugin/geb

29