Grails Unit Testing By:-  Uday & Imran
Agenda Testing overview
Understanding Unit Testing
Goals of Unit Testing
Spock Unit Testing
Writing Unit test cases
Demo
Exercise
What do we test ? What a program is supposed to do  ==  What the program actually does
Motivation People are not perfect We make errors in design and code Testing is an investment Over the time the tests build, the early investment in writing test cases pays dividends later as the size of the application grows.
A way of thinking Designing and coding are creative
Testing is destructive.The primary goal is to break the software
Very often the same person does coding and testing
Need 'split personality': when you start testing, become  paranoid  and  malicious.  Surprisingly hard to do: people don't like finding out that they make mistakes.
We have more code base to handle
Testing Testing  is a process of executing software with the intent of finding errors.
Good Testing  has high probability of finding as-yet-undiscovered errors
Successful Testing  discovers errors If it did not find any errors, need to ask whether our testing approach is good or not
Testing needs to be the integral part at each level of development Unit testing (white box)
Integration testing (white box)
Functional testing (black box)
Acceptance testing
Understanding Unit Testing Unit testing is a method by which individual units of source code are tested to determine if they are fit for use.
A unit is the smallest testable part of an application.
Each test case is independent from the others: substitutes like method stubs, mock objects, can be used to assist testing a module in isolation.
A unit test provides a strict, written contract that the piece of code must satisfy.
It tests individual methods or blocks of code without considering for surrounding infrastructure.
It is integral part of the development, so developers write unit test cases.
Shortcomings of Unit Testing Test cases ‐written to suit programmer’s implementation (not necessarily specification)
The actual database or external file is never tested directly by TDD.
It is highly reliant on Refactoring and Programmer skills
Not every code is testable.
Advantages of Unit Testing Facilitates change
Unit testing allows the programmer to refactor code at a later date, and make sure the module still works correctly
Simplifies integration
Unit testing may reduce uncertainty in the units themselves and can be used in a bottom-up testing style approach.By testing the parts of a program first and then testing the sum of its parts, integration testing becomes much easier.
Documentation
Unit testing provides a sort of living documentation of the system
Design
When software is developed using a test-driven approach, the unit test may take the place of formal design. Each unit test can be seen as a design element specifying classes, methods, and observable behaviour.
Quality Improves the quality, readability and testability of the code
Spock A developer testing framework for Java and Groovy application
Based on Groovy

Grails unit testing

Editor's Notes