Groovy
Why is worth to use Groovy?
WHY JAVA IS CUMBERSOME?
- hello world needs a class and special method
- primitive types
- arrays are primitives, collections are objects
- no closures/labmdas until Java8!
- read/write from/to file example
- use plain JDBC interface
- try to parse or generate XML, JSON without any library in few lines
- String API is limited, helper classes don't solve all problems
- lot's of useful code snipets are available in other libraries
- no scripting support
- no functional support
Groovy is like a super version of Java. It can leverage Java's enterprise capabilities but also has cool productivity features like closures, builders and dynamic typing. If you are a developer, tester or script guru, you have to love Groovy."
Groovy is
Object-oriented, dynamic, with a functional flavor
But also supports static type checking &
static compilation
GROOVY VISION
- Simplify the life of (Java) Developers
- Groovy is Java superset
- It's also easy to learn
- As safe and fast as Java with static type checking and compilation
- great for scripting
- fit for Domain-Specific Languages (DSL)
- most seamless integration & interoperability with Java
- expressive, concise readable
Web application in size of single tweet
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
return "Hello World!"
}
}
JAVA VS GROOVY
VS
println 'Hello world'
OPtional
- semicolons
- parentheses
- typing
- public keyword
- return keyword
and more sugar syntax
- handy println shorcut
- properties
- property notation
- named argument constructor
- interpoladed GStrings
NATIVE SYntax constructs
Closures
We can take a sequence of statements that refers to its external context and assign it to a variable, then execute it later. It's technically called a "closable block", commonly called a "closure":
def a = 'coffee'
def c = {
def b = 'tea'
a + ' and ' + b //a refers to the variable a outside the closure,
//and is remembered by the closure
}
assert c() == 'coffee and tea' //short for c.call()
Closures cont.
- implicit parameter 'it'
- explicit parameter types
- default parameter values
- methods as functions (& turns static method as a function)
- def printer = System.out.&println
- printer 'Hello world'
Collections-LISTS
- Lists can be evaluated as a boolean value
- We can use the each and eachWithIndex methods to execute code on each item in a list
- [1, 2, 3].each{ println "Item: $it" }
- Other useful methods added in GDK
- assert [1, 2, 3].find{ it > 1 } == 2
- assert [1,2,3,4,5,6].sum() == 21
- ssert [1, 2, 3].join('-') == '1-2-3'
- We can remove elements from a list by referring to the element/s to be removed
- assert ['a','b','c','b','b'] - 'c' == ['a','b','b','b']
COllections-LISTS cont.
- other useful methods
- assert [1,2,3,3,3,3,4,5].count(3) == 4
- assert [1,2,3] * 3 == [1,2,3,1,2,3,1,2,3]
- def stack= [1,2,4,6]; stack << 7
- assert [1,2,4,6,8,10,12].intersect([1,3,6,9,12]) == [1,6,12]
- assert ! [1,2,3].disjoint( [2,4,6] )
- map (collect), filter (findAll), reduce (inject)
and more...
COllections
- Ranges
- assert 5..8 == [5,6,7,8] //includes both values
- assert 5..<8 == [5, 6, 7] //excludes specified top value
- Sets
- def s1= [1,2,3,3,3,4] as Set
- Immutable collections
- def imList= ['a', 'b', 'c'].asImmutable()
- Maps
- def map= ['id':'FX-11', 'name':'Radish', 'no':1234, 99:'Y']
Other nice features
- Strings, GStrings, multiline strings
- lots of useful annotations, @groovy.transform.Immutable, @Memoized, @Grab, @EqualsAndHashCode, @Singleton
- decimals represented as BigDecimal
- Date ehnancements
- def today= new Date() , tomorrow= today + 1
- regular expressions
- powerful switch/case on steroids
Command chains
ability to chain method calls without parentheses and dots
NAmed arguments & command chains
FILES
String fileContents = new File('/path/to/file').text
String fileContents = new File('/path/to/file').getText('UTF-8')
new File('/path/to/file').eachLine { line -> println line }
FILES
BUIlders
-
markup, xml, json, swing
slurpers and gpath expressions
POwer asserts
Operators
Metaprogramming
Performance
Community
Killer apps
Geb
Spock
SPOCK
- Groovy DSL for testing
- Builds on JUnit 4
- Goals: Concise, maintanable
- Given/when/then
- Data driven testing (feature testing)
- Interaction based testing (mocking and stubing)
- Spock basics
SPOCK-DEMO session
Spock - other topics
- stubs
- spies
- creating extensions
- asynchronous testing
- hamcrest assertions
WHY spock
GEB
- power of Selenium Webdriver 2
- elegance of jQuery content selection
- robustness of PageObject modeling
- expressiveness of Groovy
- lead by Spock
GEB BASICS - PAGE OBJECT
- The Page type is the basis of the Page Object pattern in Geb.
- The Page type is the basis of the Page Object pattern in Geb.
GEB BAsics - Browser object
- The browser is the centre of Geb. It encapsulates a WebDriver implementation and references a Page object that provides access to the content.
- Browser objects dynamically delegate all method calls and property read/writes that it doesn't implement to the current page instance via propertyMissing() and methodMissing().
DEMO
Thank you
Groovy
By marcin
Groovy
- 912