Unit test-using-spock in Grails

50 %
50 %
Information about Unit test-using-spock in Grails

Published on February 25, 2016

Author: NexThoughts

Source: slideshare.net

1. Unit Testing Using SPOCK

2. Agenda Testing Unit Testing Spock Unit Testing Unit Test Cases Demo Practice

3. TestinG Testing is to check the equality of “What program supposed to do” and “what actually program does”. Program supposed to do == Program actually does

4. why we need testing We are not perfect, usually we do errors in coding and design. We have some wrong perception about Testing:- Designing and Coding is a creation, but testing is destruction. We don’t like finding ourselves making mistake, so avoid testing. It’s primary goal to break the software.

5. What testing does It is process of executing software with the intention of finding errors. It can help us to find undiscovered errors.

6. Type of testing Unit Testing Integration Testing Functional Testing

7. Unit testing Individual unit (function or method) of source code are tested. It is the smallest testable part of application. Each test case will be independent of others. Substitutes like mock and stubs are used.

8. Pros and cons of Unit testing Pros:- Allows refactoring at a later date and ensures module still works. Acts as a living documentation of system. Improves the quality of software. Cons:- Actual database and external files never tested directly. Highly reliant on refactoring and Programming Skills.

9. Spock A developer testing framework for Java and Groovy application. Based on Groovy. Spock lets us to write specifications that describe expected features.

10. Howto integrate spock import spock.lang.*; Package spock.lang contains the most important types for writing specifications. Need not to integrate spock after grails 2.3.* dependency{ test "org.spockframework:spock-grails-support:0.7-groovy-2.0" } plugins{ test(":spock:0.7") { exclude "spock-grails-support" } }

11. Specifications A specification is represented as a Groovy class that extends from spock.lang.Specification. class FirstTestingSpecification extends Specification{} Class names of Spock tests must end in either “Spec” or “Specification”. Otherwise the grails test runner won’t find them. It contains a number of useful methods (fixture methods) for writing specifications

12. Fixture Methods Fixture methods are responsible for setting up and cleaning up the environment in whic feature methods are run def setup() //run before every feature method def cleanup() //run after every feature method def setupSpec() //run before the first feature method def cleanupSpec() //run after the last feature method Feature Method:- def “pushing a new element in the stack”(){}

13. Blocks A test can have following blocks:- setup when //for the stimulus then //output comparison expect where

14. Example when: stack.push(elem) then: !stack.empty stack.size()>0 stack.peek()=elem

15. Expect block It is more natural to describe stimulus and expected response in a single expression:- when: def x=Math.max(1,2) then: x==2 expect: Math.max(1,2)==2

16. It is useful to test same code multiple times, with varying inputs and expected result. Data-driven testing class MathSpec extends Specification{ def “maximum of two number”(){ expect: Math.max(1,2)==2 Math.max(10,9) ==10 Math.max(0,0) ==0 class MathSpec extends Specification{ def “maximum of two number”(int a, int b, int c){ expect: Math.max(a,b)==c } }

17. Data-Table A convenient way to exercise a feature method with a fixed set of data. class DataDrivenSpec extends Specificaition{ def “max of two num”(){ expect: Math.max(a,b)==c where: a|b|c 3|5|5 7|0|7 0|0|0 } }

18. Data-pipe A data-pipe, indicated by left shift(<<) where: a<<[3, 7, 0] b<<[5, 0, 0] c<<[5, 7, 0]

19. @Unroll A method annoted with @unroll will have its iterations reported independently @Unroll def “max of two num”(){ … } @Unroll “#a and #b and result is #c”

20. Exception Condition setup: Stack stack=new Stack() when: stack.pop() then: thrown(EmptyStackException) stack.empty

21. mocking A mock is an object of certain type but without any behaviour. Calling methods on them is allowed, but have no effects other than returning the defaul value for the method’s return type. Mock objects are creatd by:- def tester = Mock(tester) Tester tester = Mock()

22. Mocking Methods mockDomain() Mock() mockConfig() mockLogging()

23. mockDomain Adds the persistence method:- get(), getAll(), exists(), count(), list(),create(), save(), validate(), delete(), discard(), add removeFrom(). mockDomain(MyDomain, []) MyDomain object = new MyDomain(name:”Vijay”) object.save()

24. Mock It can be used to mock any of the class. Return an Object of the class. MyService service = Mock() service.anyMethod(a, b)>>result

25. Test mixins Since grails 2.0, a collection of unit testing mixins is provided. e.g., @TestFor(COntroller_Name) @IgnoreRest @Mock([domain1, domain2]) @FailsWith @Timeout @Ignore

26. Testfor It defines a class under the test and automatically create a field for the type of class. e.g., @TestFor(BookController) this will create “controller” field. @TestFor(BookService) this will create “service” field.

27. Stubbing Stubbing is just providing the dummy implementation of any method. e.g., Return fixed value:- service.findName(_)>>”Nexthoughts” Return different values:- service.findName(_)>>>[“Credio”, “Investly”, “TransferGuru”] Accessing Method Argument:- service.findName(_) >>{String name-> name.lenght()>0?name:”error” } Throw an Exception:- service.findName(_) >>{throw new InternalError(“Got You!”)}

28. Questions

29. References

30. Basic Commands grails [Environment]* test-app [names]* grails test-app <file-name> You can run different test like:- grails test-app -unit (For Unit Testing) grails test-app -integration (For Integration Testing) If you want to run only failed test cases:- grails test-app -rerun To open your test-report open test-report To run only Controller grails test-app *Controller

31. Thank You Presented By:- Vijay Shukla

Add a comment

Related pages

Grails | LinkedIn

Grails ResourcesIn this Episode I will talk about where to go to find out and learn more about ... Unit test-using-spock in Grails. 0 Views. TOTHENEW ...
Read more

groovy - Unit test Groovy2.0 with Spock : setup( ) - Stack ...

I am writing unit test using Spock for ... Disabling a validation constraint in grails command object for unit testing ... Unit Testing using spock ...
Read more

groovy - Unit test Groovy2.0 with Spock : setup( ) - Stack ...

I am writing unit test using Spock for groovy-2.0 , and using gradle to run. If I write following the test pass.
Read more

Write unit tests with Spock: a tutorial to get you started ...

how easy it is do mock out collaborators of your component under test using Spock; ... 5 Responses to Write unit tests ... Questa settimana in Grails ...
Read more

Newest 'spock' Questions - Stack Overflow

Spock is a testing and specification framework for Java and Groovy applications. The highly expressive specification language makes it easy to create and ...
Read more

How to test non controller / domain / service classes in ...

... non controller / domain / service classes in Grails .. I have a Groovy helper class defined in sources/groovy which i'm trying to test using Spock ...
Read more

softIsland - blogspot.com

Brian Kotek has written great plugin to integrate CoffeeScript into Grails project ... some integration test using Spock. ... grails test-app unit: ...
Read more