0% found this document useful (0 votes)
13 views

Groovy design pattern

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Groovy design pattern

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

GStrings

As double-quoted string literals are interpreted as GString values, Groovy may fail
with
compile error or produce subtly different code if a class with String literal
containing a
dollar character is compiled with Groovy and Java compiler.
While typically, Groovy will auto-cast between GString and String if an API
declares
the type of a parameter, beware of Java APIs that accept an Object parameter and
then
check the actual type.
-String and Character literals
Singly-quoted literals in Groovy are used for String , and double-quoted result
in String or GString , depending whether there is interpolation in the literal.
assert 'c'.getClass()==String
assert "c".getClass()==String
assert "c${1}".getClass() in GString
Groovy will automatically cast a single-character String to char only when
assigning to
a variable of type char . When calling methods with arguments of type char we need
to
either cast explicitly or make sure the value has been cast in advance.
char a='a'
assert Character.digit(a, 16)==10 : 'But Groovy does boxing'
assert Character.digit((char) 'a', 16)==10
try {
assert Character.digit('a', 16)==10
assert false: 'Need explicit cast'
15/71
} catch(MissingMethodException e) {
}
Groovy supports two styles of casting and in the case of casting to char there are
subtle
differences when casting a multi-char strings. The Groovy style cast is more
lenient and will
take the first character, while the C-style cast will fail with exception.
// for single char strings, both are the same
assert ((char) "c").class==Character
assert ("c" as char).class==Character
// for multi char strings they are not
try {
((char) 'cx') == 'c'
assert false: 'will fail - not castable'
} catch(GroovyCastException e) {
}
assert ('cx' as char) == 'c'
assert 'cx'.asType(char) == 'c'
-Behaviour of ==
In Java == means equality of primitive types or identity for objects. In
Groovy == translates to a.compareTo(b)==0 , if they are Comparable ,
and a.equals(b) otherwise. To check for identity, there is is . E.g. a.is(b) .
Question: How To Test Groovy Application?
The Groovy programming language comes with great support for writing tests. In
addition
to the language features and test integration with state-of-the-art testing
libraries and
frameworks.
The Groovy ecosystem has born a rich set of testing libraries and frameworks.
Groovy Provides following testing capabilities
Junit Integrations
Spock for specifications
Geb for Functional Test
Groovy also has excellent built-in support for a range of mocking and stubbing
alternatives.
When using Java, dynamic mocking frameworks are very popular.
A key reason for this is that it is hard work creating custom hand-crafted mocks
using Java.
Such frameworks can be used easily with Groovy.
Question: What Are Power Assertions In Groovy?
16/71
Writing tests means formulating assumptions by using assertions. In Java this can
be done
by using the assert keyword. But Groovy comes with a powerful variant of assert
also
known as power assertion statement.
Groovy’s power assert differs from the Java version in its output given the boolean
expression validates to false :
def x = 1
assert x == 2
// Output:
//
// Assertion failed:
// assert x == 2
// | |
// 1 false
This section shows the std-err output
The java.lang.AssertionError that is thrown whenever the assertion can not be
validated successfully, contains an extended version of the original exception
message.
The power assertion output shows evaluation results from the outer to the inner
expression.
The power assertion statements true power unleashes in complex Boolean statements,
or
statements with collections or other toString -enabled classes:
def x = [1,2,3,4,5]
assert (x << 6) == [6,7,8,9,10]
// Output:
//
// Assertion failed:
// assert (x << 6) == [6,7,8,9,10]
// | | |
// | | false
// | [1, 2, 3, 4, 5, 6]
// [1, 2, 3, 4, 5, 6]
Question: Can We Use Design Patterns In Groovy?
Design patterns can also be used with Groovy. Here are important points
Some patterns carry over directly (and can make use of normal Groovy syntax
improvements for greater readability)
Some patterns are no longer required because they are built right into the language
or because Groovy supports a better way of achieving the intent of the pattern
some patterns that have to be expressed at the design level in other languages can
be implemented directly in Groovy (due to the way Groovy can blur the distinction
between design and implementation)
Question: How To Parse And Produce JSON Object In Groovy?
17/71
Groovy comes with integrated support for converting between Groovy objects and
JSON.
The classes dedicated to JSON serialisation and parsing are found in
the groovy.json package.
JsonSlurper is a class that parses JSON text or reader content into Groovy data
structures (objects) such as maps, lists and primitive types
like Integer , Double , Boolean and String .
The class comes with a bunch of overloaded parse methods plus some special methods
such as parseText , parseFile and others

You might also like